首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >当字符串中有行中断时,oracle中的SUBSTR无法工作

当字符串中有行中断时,oracle中的SUBSTR无法工作
EN

Stack Overflow用户
提问于 2020-05-15 10:56:20
回答 3查看 254关注 0票数 0

好的,我正在处理一个数据库,它有很多格式的数据。我被要求在每列中将字符串分成3列,长度为150。

表中的数据存储如下

代码语言:javascript
运行
复制
'1. These MSDS, Declaration and SGS are to post to Environment database.
        The MSDS serial number is MCSPRINGH0403021.
        The Declaration serial number is DCSPRINGH0403004.
        The SGS serial number is TCSPRINGH0403007.
    2. It's required to follow specification drawing with Rev.A0 for MP.
        The change for Rev.A0 is only change Rev."PA4" to "A0"  on silkscreen printing.


        '

我想把这些数据分成3列,COL1,COL2,COL3

当我使用这个查询时,它工作得很好。

代码语言:javascript
运行
复制
SELECT SUBSTR('1. These MSDS, Declaration and SGS are to post to Environment database.
    The MSDS serial number is MCSPRINGH0403021.
    The Declaration serial number is DCSPRINGH0403004.
    The SGS serial number is TCSPRINGH0403007.
2. It''s required to follow specification drawing with Rev.A0 for MP.
    The change for Rev.A0 is only change Rev."PA4" to "A0"  on silkscreen printing.


    ', 1, 140) col1, 
    SUBSTR('1. These MSDS, Declaration and SGS are to post to Environment database.
    The MSDS serial number is MCSPRINGH0403021.
    The Declaration serial number is DCSPRINGH0403004.
    The SGS serial number is TCSPRINGH0403007.
2. It''s required to follow specification drawing with Rev.A0 for MP.
    The change for Rev.A0 is only change Rev."PA4" to "A0"  on silkscreen printing.


    ', 141, 280) col2,
    SUBSTR('1. These MSDS, Declaration and SGS are to post to Environment database.
    The MSDS serial number is MCSPRINGH0403021.
    The Declaration serial number is DCSPRINGH0403004.
    The SGS serial number is TCSPRINGH0403007.
2. It''s required to follow specification drawing with Rev.A0 for MP.
    The change for Rev.A0 is only change Rev."PA4" to "A0"  on silkscreen printing.


    ', 281, 420) col3 FROM DUAL

但我不得不用It''s代替It''s。如何替换存储在列中的值,但这不会将'作为字符计算,最终会返回错误的计数。此外,当有一个行中断,我希望它被作为2字符/b

干杯!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-05-15 11:05:02

当您实际将数据表示为查询或DML命令中的列时,您就不会遇到这个问题。换句话说,当你这样做的时候:

代码语言:javascript
运行
复制
select original_col,
       substr(original_col,1,140) col1,
       substr(original_col,141,140) col2,
       substr(original_col,281,140) col3
  from table;

它应该能正常工作。当您自己键入文字文本时,您只需要担心转义特殊字符,就像在您的示例中一样。

另外,要知道substr中的第二个数字是字符串的长度,而不是段的结尾位置,所以如果您想要140个字符,应该始终是140个字符。从文档中,“SUBSTR函数返回字符的一部分,从字符位置开始,substring_length字符长。”

https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/SUBSTR.html#GUID-C8A20B57-C647-4649-A379-8651AA97187E

票数 5
EN

Stack Overflow用户

发布于 2020-05-15 11:12:40

当您要使用substr中的列名时,您不需要担心,因为您面临的问题是字符串创建,而不是substr

在使用字符串时,可以使用引用的字符串,如下所示:

代码语言:javascript
运行
复制
q'<delimiter><Your string with><delimeter>'

可以使用特殊字符作为分隔符。

代码语言:javascript
运行
复制
q'#That's it#' -- > That's it

您必须从oracle文档中了解引用的字符串。

您还可以使用两个单引号,这将被视为单引号,同时准备字符串。所以你的

代码语言:javascript
运行
复制
That''s it --> That's it
票数 0
EN

Stack Overflow用户

发布于 2020-05-15 11:13:13

使用Q-引用机制.val_1是您目前所做的,val_2是一个新的建议:

代码语言:javascript
运行
复制
SQL> select substr( '1. This is MSDS, it''s something special' , 1, 30) val_1,
  2         substr(q'[1. This is MSDS, it's something special]', 1, 30) val_2
  3  from dual;

VAL_1                          VAL_2
------------------------------ ------------------------------
1. This is MSDS, it's somethin 1. This is MSDS, it's somethin

SQL>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61817571

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档