Solved

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

Posted on 2014-03-28
7
218 Views
Last Modified: 2014-03-31
Hi,

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
MyTown
AA1 1AA"

becomes

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.

Thanks,

Paul
0
Comment
Question by:PaulBS
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 9

Expert Comment

by:edtechdba
ID: 39962007
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.
0
 

Author Comment

by:PaulBS
ID: 39962026
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)
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 39962119
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).
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Author Comment

by:PaulBS
ID: 39962156
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.
Cheers
Paul
0
 
LVL 41

Expert Comment

by:Sharath
ID: 39962253
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
MyTown
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
	MyTown
	AA1 1AA
	*/

Open in new window

0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 250 total points
ID: 39962487
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 ...



SELECT
    strAddress,    
    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    
FROM (
    SELECT '34 West Road
Little Villas
MyTown
AA1 1AA' AS strAddress UNION ALL
    SELECT '43 East Road
Big Villas
YourTown
BB2 2BB'
) AS test_data
CROSS APPLY (
    SELECT CHARINDEX(CHAR(10), strAddress) AS endAddrLine1
) AS ca1
CROSS APPLY (
    SELECT CHARINDEX(CHAR(10), strAddress, endAddrLine1 + 1) AS endAddrLine2
) AS ca2
CROSS APPLY (
    SELECT CHARINDEX(CHAR(10), strAddress, endAddrLine2 + 1) AS endAddrLine3
) AS ca3
0
 

Author Closing Comment

by:PaulBS
ID: 39967353
Thanks for your help, all looks good and gives me a place to get going with.
0

Featured Post

 Watch the Recording: Learning MySQL 5.7

MySQL 5.7 has a lot of new features. If you've dabbled with an older version of MySQL, it is definitely worth learning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question