假设我有一个列在执行查询后插入了以下字符串:
"WE welcome you all to the ceremony"
现在我想要第一行中的前15个字符和下一行中的剩余字符,如下所示:
WE welcome you
all to the ceremony
请帮帮我。
答案 0 :(得分:0)
SUBSTR能够做到这一点:
SQL> with test as (select 'WE welcome you all to the ceremony' col from dual)
2 select substr(col, 1, 15) ||
3 chr(10) ||
4 substr(col, 16) result
5 from test;
RESULT
-----------------------------------
WE welcome you
all to the ceremony
然而,对于不同的输入字符串,事情可能会改变并且看起来很愚蠢:
SQL> with test as (select 'WE welcome Harry Potter to the ceremony' col from dual)
2 select substr(col, 1, 15) ||
3 chr(10) ||
4 substr(col, 16) result
5 from test;
RESULT
----------------------------------------
WE welcome Harr
y Potter to the ceremony
在这种情况下你想做什么?一种选择是在第15个字符之前在第一个空格上分割一个字符串;例如:
SQL> with test as (select 'WE welcome Harry Potter to the ceremony' col from dual)
2 select
3 substr(col, 1, instr(col, ' ', 1, regexp_count(substr(col, 1, 15), ' '))) ||
4 chr(10) ||
5 substr(col, instr(col, ' ', 1, regexp_count(substr(col, 1, 15), ' ')) + 1) result
6 from test;
RESULT
----------------------------------------
WE welcome
Harry Potter to the ceremony
其他选项也是可能的,但您应该解释在哪种情况下该怎么做。