-2

I DO NOT need to create the 2 new fields to move the data to. The fields have already been created. I just need to move the comma-separated data from 1 field into the 2 fields that have already been created

I have 1 column with city and state data. I need to move the data from that 1 column into the 2 columns- one for city and for state.

I need to take the comma-split data from Address1 and place into Address2 and Address3 column.

Address1 (column)
Anywhere, USA

Address2 (column)  Address3 (column)
Anywhere            USA

1 Answers1

0

You may use solution from How to split a comma-separated value to columns to create an UPDATE statement:

CREATE TABLE #test (Address1 varchar(100),Address2 varchar(100),Address3 varchar(100))

INSERT INTO #test (Address1) VALUES ('Anywhere, USA')
INSERT INTO #test (Address1) VALUES ('Anywhere')

UPDATE #test set
Address2 = Substring(Address1 , 1,Charindex(',', Address1)-1),
Address3 = Substring(Address1 , Charindex(',', Address1 )+1, LEN(Address1))
WHERE Address1 LIKE '%,%'

SELECT * FROM #test
DROP TABLE #test
Piotr Palka
  • 3,086
  • 1
  • 9
  • 17