数据长度在特定字段中是随机的,如下所示:
我只想选择特殊字符之前的字符。
样品:
123456_3
454656484784_13
15051514745_7
预期结果如下:
123456
454656484784
15051514745
获得第一部分
select substr(your_column, 1, INSTR(your_column, '_') - 1)
from your_table
得到最后一部分
select substr(your_column, INSTR(your_column, '_') + 1)
from your_table
一般示例:
substring(your_data, 0, instr(your_data, '_'))
快速而肮脏 - 您可以通过 Oracle/Sybase 程序或类似的程序一步一步弄清楚如何做到这一点......
-- Ignore 0 or negative numbers --
-- This gives you your end pos for substr --
SELECT distinct(Instr('123456_3 454656484784_13 15051514745_7', '_', LEVEL)-1) substr_end_pos
FROM dual
CONNECT BY LEVEL < length('123456_3 454656484784_13 15051514745_7')
/
-- This is start pos for substr --
SELECT Distinct(Instr('123456_3 454656484784_13 15051514745_7', ' ', LEVEL)+1) substr_start_pos
FROM dual
CONNECT BY LEVEL < length('123456_3 454656484784_13 15051514745_7')
/
-- These are substr start/end numbers returned by above queries --
Select Substr('123456_3 454656484784_13 15051514745_7', 1, 6) From dual -- substr_start_pos=1, substr_end_pos=6
/
Select Substr('123456_3 454656484784_13 15051514745_7', 10, 21-9) From dual -- substr_start_pos=10, substr_end_pos=21-substr_start_pos-1
/
Select Substr('123456_3 454656484784_13 15051514745_7', 26, 36-25) From dual -- substr_start_pos=26 substr_end_pos=36-substr_start_pos-1
/