0

I am trying to split a string(eg. BUFORD GA 30518) based on spaces to fill into different fields City, State and zipcode. Please help me in writing Query for this in SQL

1 Answers1

0

Try this:

SELECT  City= LEFT(Column, CHARINDEX(' ',Column)-1)
,State= LEFT(SUBSTRING(Column, CHARINDEX(' ',Column)+1,LEN(Column)), CHARINDEX(' ',SUBSTRING(Column, CHARINDEX(' ',Column)+1,LEN(Column)))-1)
,zipcode= REVERSE(LEFT(REVERSE(Column), CHARINDEX(' ',REVERSE(Column))-1))

You can try this:

SELECT REGEXP_SUBSTR(t.column_one, '[^ ]+', 1, 1) col_one,
       REGEXP_SUBSTR(t.column_one, '[^ ]+', 1, 2) col_two,
       REGEXP_SUBSTR(t.column_one, '[^ ]+', 1, 3) col_three
FROM YOUR_TABLE t;

Can try this also:

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,
       SUBSTR(t.column_one, INSTR(t.column_one, ' ')+1) AS col_three
FROM YOUR_TABLE t
Jibin Balachandran
  • 3,381
  • 1
  • 24
  • 38