I have a column name called as 'STATS1' in one of my Oracle DBs. Following are the field values that are under this column -

010112.TMD.ORG1
010112.F99.DEFAULT
010112.F20
010112.F16.ORG2.XYZ12

Now, I want to write an sql query to split the above strings into multiple. Below is the O/P format that I am seeking for using SQL substr and instr functions.

ACCOUNT     GRPID   ORG     REL
=======     =====   ====    ====
010112      TMD     ORG1
010112      F99     DEFAULT
010112      F20
010112      F16     ORG2    XYZ12
有帮助吗?

解决方案

select 
  regexp_substr(column, '[^.]+', 1, 1) as account
, regexp_substr(column, '[^.]+', 1, 2) as grpid
, regexp_substr(column, '[^.]+', 1, 3) as org
, regexp_substr(column, '[^.]+', 1, 4) as rel
from your_table

其他提示

Just to show a less elegant solution by using SUBSTR and INSTR instead of REGEXP_SUBSTR :-) You can add delimiters to your column in order to use INSTR without any danger of not finding the nth delimiter.

with formatted as
(
  select '.' || col || '....' as col
  from mytable
)
select 
  substr(col, instr(col, '.', 1, 1) + 1, instr(col, '.', 1, 2) - instr(col, '.', 1, 1) - 1) as aaa,
  substr(col, instr(col, '.', 1, 2) + 1, instr(col, '.', 1, 3) - instr(col, '.', 1, 2) - 1) as aaa,
  substr(col, instr(col, '.', 1, 3) + 1, instr(col, '.', 1, 4) - instr(col, '.', 1, 3) - 1) as aaa,
  substr(col, instr(col, '.', 1, 4) + 1, instr(col, '.', 1, 5) - instr(col, '.', 1, 4) - 1) as aaa
from formatted;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top