0

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.

i.e 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

2 Answers 2

1

You can use regexp_substr(), probably with trim():

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

Here is a db<>fiddle.

Sign up to request clarification or add additional context in comments.

Comments

1

I think you need some basic SUBSTR & INSTR function -

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.