Split varchar into separate columns in Oracle

前端 未结 3 759
难免孤独
难免孤独 2020-12-13 20:10

I\'m in a bit of a pickle: I\'ve been asked to take in comments starting with a specific string from a database, and separate the result into separate columns.

For

3条回答
  •  臣服心动
    2020-12-13 20:59

    Depends on the consistency of the data - assuming a single space is the separator between what you want to appear in column one vs two:

    SELECT SUBSTR(t.column_one, 1, INSTR(t.column_one, ' ')-1) AS col_one,
           SUBSTR(t.column_one, INSTR(t.column_one, ' ')+1) AS col_two
      FROM YOUR_TABLE t
    

    Oracle 10g+ has regex support, allowing more flexibility depending on the situation you need to solve. It also has a regex substring method...

    Reference:

    • SUBSTR
    • INSTR

提交回复
热议问题