简体   繁体   English

根据逗号将一列拆分为多列(Oracle)

[英]Split a column into multiple columns based on comma (Oracle)

I'm creating a new table and carrying over several fields from a previous table.我正在创建一个新表并继承前一个表中的几个字段。 One of the fields is "Address" that needs to be split into several columns based on comma in the new table.其中一个字段是“地址”,需要根据新表中的逗号将其拆分为几列。

ie current column即当前列

      Clientid         Address
      1                123 E 123th st, APT 4L
      2                17 E16th st, APT 3B

newly created columns:新创建的列:

       Clientid                address1                 address2
       1                       123 E123th st            APT 4L
       2                       17 E 16th st             APT 3B

My question is is this even possible without hardcoding?我的问题是,如果没有硬编码,这是否可能? Since I can't tell how many characters away the comma is for each individual record I'm assuming I'll need to come up with some sort of loop to check the condition?由于我不知道逗号对于每个单独的记录有多少个字符,我假设我需要想出某种循环来检查条件?

Thanks谢谢

You can use regexp_substr() , probably with trim() :您可以使用regexp_substr() ,可能与trim()

select trim(regexp_substr(address, '[^,]+', 1)),
       trim(regexp_substr(address, '[^,]+', 1, 2))

Here is a db<>fiddle. 是一个 db<>fiddle。

I think you need some basic SUBSTR & INSTR function -我认为您需要一些基本的 SUBSTR 和 INSTR function -

SELECT Clientid,
       SUBSTR(Address, 1, INSTR(Address, ',') - 1) address1,
       SUBSTR(Address, INSTR(Address, ',') + 1, LENGTH(Address)) address2
  FROM YOUR_TABLE;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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