好的,所以我正在使用一个包含多种格式数据的数据库。我被要求将一个字符串分成 3 列,每列长度为 150
表中的数据是这样存储的
'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.
'
Run Code Online (Sandbox Code Playgroud)
我想将此数据拆分为 3 列,COL1、COL2、COL3
当我使用此查询时,它工作正常。
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
Run Code Online (Sandbox Code Playgroud)
但我不得不替换It's为It''s. 当值存储在列中时如何替换,但这不会算作'一个字符,最终会返回错误的计数。此外,当有换行符时,我希望将其作为 2 个字符/b。
干杯!
当您在查询或 DML 命令中实际将数据表示为一列时,您就不应该有这个问题。换句话说,当你这样做时:
select original_col,
substr(original_col,1,140) col1,
substr(original_col,141,140) col2,
substr(original_col,281,140) col3
from table;
Run Code Online (Sandbox Code Playgroud)
它应该可以正常工作。当您自己输入文字时,您只需担心转义特殊字符,如您的示例所示。
另外,请注意 substr 中的第二个数字是字符串的长度,而不是段的结束位置,所以如果你想要 140 个字符,这应该总是140。从文档中,“SUBSTR 函数返回一部分char,从字符位置开始,substring_length 个字符长。”
| 归档时间: |
|
| 查看次数: |
103 次 |
| 最近记录: |