【问题标题】:Is there a possibility to split a cell data to multiple columns in Oracle是否有可能将单元格数据拆分为 Oracle 中的多个列
【发布时间】:2020-09-06 11:01:33
【问题描述】:

我的表中有一个 clob 列,其数据如下所示

Name: ABC
Place: XYZ
Age: 123
Role: Developer
Skill: SQL

表 emp 中有数百条记录,其中每个 emp_id 都会有一个列和上面的描述。 现在我想将此数据显示为 emp id 的每一列。 像这样的

select emp_id, name, place, age, role, skill from emp where emp_id=xx;

使用reg exp 可以做到这一点吗?或任何其他方式?请提出建议。

【问题讨论】:

  • 嗯...不是很清楚。您提到的同一个 EMP 表中的那个 CLOB 列吗?该 CLOB 列中的 EMP_ID 在哪里(以便您可以将其与其余数据加入)?您能否解释一下,甚至可以发布更多示例数据来说明问题?
  • 如果您将其存储为 JSON 值,会更容易很多。您可以使用 Oracle JSON 函数轻松提取值

标签: sql regex oracle plsql split


【解决方案1】:

是的,可以在 CLOB 列上使用 REGEXP_SUBSTR:

OPS$ORACLE@FTEX>desc emp;
 Name                                                              Null?    Type
 ----------------------------------------------------------------- -------- --------------------------------------------
 EMP_ID                                                                     NUMBER
 X                                                                          CLOB

OPS$ORACLE@FTEX>select * from emp;

    EMP_ID X
---------- --------------------------------------------------------------------------------
         1 Name: ABC Place: XYZ Age: 123 Role: Developer Skill: SQL

OPS$ORACLE@FTEX>create or replace view v_emp as
  2  (
  3  select emp_id,
  4         regexp_substr(x, '(\S*)(\:)(\s)(\S*)', 1, 1, null, 4) as Name,
  5         regexp_substr(x, '(\S*)(\:)(\s)(\S*)', 1, 2, null, 4) as Place,
  6         regexp_substr(x, '(\S*)(\:)(\s)(\S*)', 1, 3, null, 4) as Age,
  7         regexp_substr(x, '(\S*)(\:)(\s)(\S*)', 1, 4, null, 4) as Role,
  8         regexp_substr(x, '(\S*)(\:)(\s)(\S*)', 1, 5, null, 4) as Skill
  9         from emp
 10  );

View created.

OPS$ORACLE@FTEX>select * from v_emp;

    EMP_ID NAME                 PLACE                AGE   ROLE                 SKILL
---------- -------------------- -------------------- ----- -------------------- --------------------
         1 ABC                  XYZ                  123   Developer            SQL

OPS$ORACLE@FTEX>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-24
    相关资源
    最近更新 更多