简体   繁体   中英

Split multiple values in a cell to multiple rows - Oracle SQL

I have a table:

table1

values
------------
x=new letter
------------
a=old letter
ba=older letter
xq=newer letter
------------
xf=new apple
xt=new orange
x3=new fruit
xtt=new seed

I have to separate the values in each cell to multiple rows.

The following is the output:

table2

code      description
x         new letter
a         old letter
ba        older letter
xq        newer letter
xf        new apple
xt        new orange
x3        new fruit
xtt       new seed

How can this be achieved?

try like below

SELECT NVL(SUBSTR('a=old letter', 0, INSTR('a=old letter', '=')-1), 'a=old letter') 
AS col1, NVL(SUBSTR('a=old letter', INSTR('a=old letter', '=')+1), 'a=old letter')    
  FROM DUAL

so in you case

SELECT NVL(SUBSTR(values, 0, INSTR(values, '=')-1), values) 
AS col1, NVL(SUBSTR(values, INSTR(values, '=')+1), values) 

  FROM table1

I would use regexp_replace() or regexp_substr() :

select regexp_substr(str, '^[^=]+') as code,
       regexp_substr(str, '[^=]+$') as value

Here is a db<>fiddle.

Note that this does not use values for the column name. That is a very bad choice for a column name because it is a SQL keyword.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM