将单元格中的多个值拆分为多行-Oracle SQL

时间:2019-03-08 09:53:55

标签: sql oracle split

我有一张桌子:

表1

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

我必须将每个单元格中的值分成多行。

以下是输出:

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

如何实现?

2 个答案:

答案 0 :(得分:2)

尝试如下

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

所以在您的情况下

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

  FROM table1

答案 1 :(得分:2)

我会使用regexp_replace()regexp_substr()

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

Here是db <>小提琴。

请注意,此名称不使用values作为列名。对于列名,这是一个非常糟糕的选择,因为它是一个SQL关键字。