【问题标题】:How to select second split of column data from oracle database如何从 oracle 数据库中选择第二次拆分列数据
【发布时间】:2019-09-19 22:49:06
【问题描述】:

我想从 Oracle 表中选择数据,而表列包含的数据为 , [ex : key,value] 分隔值;所以在这里我想选择第二个拆分,即值

表格列数据如下:

column_data
++++++++++++++
asper,worse
tincher,good
golder
null                       -- null values need to eliminate while selection
www,ewe

根据上述数据,所需的输出如下:

column_data
+++++++++++++
worse
good
golder
ewe

请帮我查询

【问题讨论】:

  • 不要将列记录存储为逗号分隔值。它一定会给你带来永远的问题。
  • 我会支持@KaushikNayak 的评论。你展示的东西违反了人类已知的每一个设计原则。您需要花一些时间研究“数据规范化”和“第三范式”。获取所需数据的困难是糟糕的表格设计的直接结果。

标签: oracle


【解决方案1】:

根据您提供的数据,这里有两种选择:

  • result1:正则表达式一(如果存在则取第二个word;否则取第一个)
  • result2:SUBSTR+INSTR组合

SQL> with test (col) as
  2    (select 'asper,worse'  from dual union all
  3     select 'tincher,good' from dual union all
  4     select 'golder'       from dual union all
  5     select null           from dual union all
  6     select 'www,ewe'      from dual
  7    )
  8  select col,
  9         nvl(regexp_substr(col, '\w+', 1, 2), regexp_substr(col, '\w+', 1,1 )) result1,
 10         --
 11         nvl(substr(col, instr(col, ',') + 1), col) result2
 12  from test
 13  where col is not null;

COL          RESULT1              RESULT2
------------ -------------------- --------------------
asper,worse  worse                worse
tincher,good good                 good
golder       golder               golder
www,ewe      ewe                  ewe

SQL>

【讨论】:

  • "根据您提供的数据,这里有两个选项:" 实际上 topicstarter 请记住 nvl(substr(col, instr(col, ',') + 1) 不适用于 asper,worse,other,因为它会选择 @987654328 @
  • 嗨,LF,感谢您的回复;我的表数据只包含键、值;我需要将结果作为第一行的第一次拆分和第二行的第二次拆分;如果数据不包含,分隔符;我也必须选择它并将其放在一行中
  • 不客气。我写的查询返回您最初描述的结果。如果要求与此不同,您介意发布您所说的示例吗?请编辑问题以执行此操作并发表评论(否则将没有警报,我不会查看它)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-19
相关资源
最近更新 更多