I am migrating data from DB1 to DB2 via csv.
In DB1 there is only one address field 'address' that contains carriage returns between each address line.
DB2 however has three fields for the address going into the csv as Addr-01, Addr-02, Addr-03
How do I split this one field into three fields?
It has to be three even though DB1 address may have four or five 'lines' so any extra will just have to be concatenated into address3.
This is roughly the query
I wrote a function that returned a table before I found it had to be used in a JOIN which meant a record for each address line. This is the function
In DB1 there is only one address field 'address' that contains carriage returns between each address line.
DB2 however has three fields for the address going into the csv as Addr-01, Addr-02, Addr-03
How do I split this one field into three fields?
It has to be three even though DB1 address may have four or five 'lines' so any extra will just have to be concatenated into address3.
This is roughly the query
Code:
SELECT customer 'Account', full_name Name, address 'Addr-01', --Addr-02, --Addr-03, city 'Town', county 'County', postcode 'Post Code' FROM customer
FUNCTION splitString (@delim CHAR(1), @str VARCHAR (100))
RETURNS @t TABLE (line VARCHAR(50))
AS
BEGIN
DECLARE @pos INT
DECLARE @piece VARCHAR(50)
IF RIGHT(RTRIM(@st r),1) <> @delim
SET @str = @str + @delim
SET @pos = CHARINDEX(@deli m, @str)
WHILE @pos <> 0
BEGIN
SET @piece = LEFT(@str, @pos - 1)
INSERT INTO @t VALUES(CAST(@pi ece AS VARCHAR(50)))
SET @str = STUFF(@str, 1, @pos, '')
SET @pos = CHARINDEX(@deli m , @str)
END
RETURN
END
RETURNS @t TABLE (line VARCHAR(50))
AS
BEGIN
DECLARE @pos INT
DECLARE @piece VARCHAR(50)
IF RIGHT(RTRIM(@st r),1) <> @delim
SET @str = @str + @delim
SET @pos = CHARINDEX(@deli m, @str)
WHILE @pos <> 0
BEGIN
SET @piece = LEFT(@str, @pos - 1)
INSERT INTO @t VALUES(CAST(@pi ece AS VARCHAR(50)))
SET @str = STUFF(@str, 1, @pos, '')
SET @pos = CHARINDEX(@deli m , @str)
END
RETURN
END
Comment