SQL View to show separate fields from one which has multiple lines


I have a table (inherited) which I cannot change but which stores a postal address in a single field with line breaks (for readability I presume).

I want to create a query/view in SQL that will create separate "fields" for each line in the address.

eg: strAddress =
"34 West Road
Little Villas
AA1 1AA"


strAddress1 = "34 West Road"
strAddress2 = "Little Villas"
strTown = "MyTown"
strPostCode = "AA1 1AA"

(field names are examples only).

I am OK with most SQL but this has gone above my head and my searches have found lots of potential answers but nothing close enough.


Paul StevensonMarketing and IT ManagerAsked:
Who is Participating?
Scott PletcherConnect With a Mentor Senior DBACommented:
If it's always consistent, you really don't need a specialized splitter, you can just use CROSS APPLYs.

If it's possible that an address(es) could be missing, you'll need to add the appropriate CASE WHEN endAddrLine# = 0 THEN ...

    REPLACE(LEFT(strAddress, endAddrLine1 - 1), CHAR(13), '') AS addrLine1,
    REPLACE(SUBSTRING(strAddress, endAddrLine1 + 1, endAddrLine2 - endAddrLine1), CHAR(13), '') AS addrLine2,
    REPLACE(SUBSTRING(strAddress, endAddrLine2 + 1, endAddrLine3 - endAddrLine2), CHAR(13), '') AS addrLine3,
    REPLACE(SUBSTRING(strAddress, endAddrLine3 + 1, 1000), CHAR(13), '') AS addrLine4    
    SELECT '34 West Road
Little Villas
AA1 1AA' AS strAddress UNION ALL
    SELECT '43 East Road
Big Villas
BB2 2BB'
) AS test_data
    SELECT CHARINDEX(CHAR(10), strAddress) AS endAddrLine1
) AS ca1
    SELECT CHARINDEX(CHAR(10), strAddress, endAddrLine1 + 1) AS endAddrLine2
) AS ca2
    SELECT CHARINDEX(CHAR(10), strAddress, endAddrLine2 + 1) AS endAddrLine3
) AS ca3
Is there any type of delimiter within the string in order to parse properly?

Such as a comma? See below.
"34 West Road, Little Villas, MyTown, AA1 1AA"

Or a set position for each piece of the address?
For example, address line 1 starts at position 1 and ends at position 12.
Paul StevensonMarketing and IT ManagerAuthor Commented:
Unfortunately no comma, just the new line (LF or CR can't tell which).
Commas are used but not as needed (just part of an address)
A proven path to a career in data science

At Springboard, we know how to get you a job in data science. With Springboard’s Data Science Career Track, you’ll master data science  with a curriculum built by industry experts. You’ll work on real projects, and get 1-on-1 mentorship from a data scientist.

Scott PletcherSenior DBACommented:
It's easy enough to split the column into string1, string2, etc..  An inline-table-valued function gives by far the best performance.  If you want an excellent splitter and sample code to invoke it, let me know.

But determining what part of the address each string contains is vastly more complicated.  You really need specialized software for that (such as the address parsing that Google offers you).
Paul StevensonMarketing and IT ManagerAuthor Commented:
Hi, yes an example would be very helpful.
Luckily the fields are quite consistent so no need to identify the fields, they are always in the same place.
SharathData EngineerCommented:
I don't know your data much. Based on sample given, you can try like this.
I assume you don't have more than 4 lines of address and dot (.) is not part of your address. Otherwise, let me know with more examples.
declare @strAddress nvarchar(max)
select @strAddress = 
'34 West Road
Little Villas
AA1 1AA'
select PARSENAME(strAddress,4) strAddress1,
       PARSENAME(strAddress,3) strAddress2,
	   PARSENAME(strAddress,2) strTown,
	   PARSENAME(strAddress,1) strPostCode
  from (select replace(@strAddress,char(10),'.') strAddress) t1

strAddress1	strAddress2	strTown	strPostCode
34 West Road
	Little Villas
	AA1 1AA

Open in new window

Paul StevensonMarketing and IT ManagerAuthor Commented:
Thanks for your help, all looks good and gives me a place to get going with.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.