Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Selecting Data seperated by commas

Author  Topic 

mflammia
Starting Member

44 Posts

Posted - 2008-06-03 : 11:20:02
Have a column in my DB called _Venue, which has been populated with address information separated by commas, like this:

Address1,address2,address3,address4

The address has sometimes only a few lines, for example I may have just Address1 or just address1 and address2 filled etc.

I need to write a query that will individually select addressX, so I can pick and choose what part of the full address to use.

Can these be done?

Thanks in advance.

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2008-06-03 : 11:32:22
one option is to use sql's builtin PARSENAME function:

declare @a varchar(200)
set @a = 'Address1,address2,address3,address4'
select parsename(replace(@a,',','.'), 4)
select parsename(replace(@a,',','.'), 3)
select parsename(replace(@a,',','.'), 2)
select parsename(replace(@a,',','.'), 1)


Be One with the Optimizer
TG
Go to Top of Page

mflammia
Starting Member

44 Posts

Posted - 2008-06-04 : 04:27:14
Hi thanks for replying. My apologies, but I may not of explained this correctly or I have simply not understood the code provided properly.

The name of the field in the DB is called _Venue and in side that is address data like

Row 1 Layer Marney Tower
Row 2 High Chelmer Shopping Centre, Chelmsford
Row 3 Epping Forest Visitor Centre, Paul's Nursery Road, Loughton, Essex IG10 4A

So I am not sure what to add into the line

set @a = ‘address1,address2,address3,address4’

as the information and the format it always different.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-04 : 05:07:07
quote:
Originally posted by mflammia

Hi thanks for replying. My apologies, but I may not of explained this correctly or I have simply not understood the code provided properly.

The name of the field in the DB is called _Venue and in side that is address data like

Row 1 Layer Marney Tower
Row 2 High Chelmer Shopping Centre, Chelmsford
Row 3 Epping Forest Visitor Centre, Paul's Nursery Road, Loughton, Essex IG10 4A

So I am not sure what to add into the line

set @a = ‘address1,address2,address3,address4’

as the information and the format it always different.



TG gave the string as a sample data. you need to apply it on your table as follows
SELECT PARSENAME(REPLACE(addressfield,' ','.'),4),
PARSENAME(REPLACE(addressfield,' ','.'),3),
PARSENAME(REPLACE(addressfield,' ','.'),2),
PARSENAME(REPLACE(addressfield,' ','.'),1)
FROM YourTable
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2008-06-04 : 08:53:27
visakh16 had a misplaced comma in there but...here is an example closer to your (reported) situation. Since you have anywhere from 1 to 4 address components in your values and since PARSENAME starts with the fourth position this may not be optimal for you. If you can't figure out how to display this data exactly as you need, post a more specific requirment. ie desired results based on your sample data.


declare @t table (row int, _Venue varchar(100))
insert @t
Select 1, 'Layer Marney Tower' union all
Select 2, 'High Chelmer Shopping Centre, Chelmsford' union all
Select 3, 'Epping Forest Visitor Centre, Paul''s Nursery Road, Loughton, Essex IG10 4A'

SELECT PARSENAME(REPLACE(_Venue,',', '.'),4) address1
,PARSENAME(REPLACE(_Venue, ',', '.'),3) address2
,PARSENAME(REPLACE(_Venue, ',', '.'),2) address3
,PARSENAME(REPLACE(_Venue, ',', '.'),1) address4
FROM @t

address1 address2 address3 address4
------------------------------------------------------------------------------------------------------------
NULL NULL NULL Layer Marney Tower
NULL NULL High Chelmer Shopping Centre Chelmsford
Epping Forest Visitor Centre Paul's Nursery Road Loughton Essex IG10 4A


Be One with the Optimizer
TG
Go to Top of Page

mflammia
Starting Member

44 Posts

Posted - 2008-06-04 : 12:25:36
Great! That worked perfect. Thanks very much.
Go to Top of Page

mflammia
Starting Member

44 Posts

Posted - 2008-06-04 : 17:33:27
Wanted to extend this query a little and needed a bit more help. Would like to fill any NULL returned on address1 with a hyphen and thought I could do it like this:

Select
Case
When address1 is null
Then '-'
Else address1
End

But having a bit of trouble integrating this code with the above and getting it to work?

Thanks.
Go to Top of Page

Kumar_Anil
Yak Posting Veteran

68 Posts

Posted - 2008-06-04 : 17:55:25
Use Coalesce & that will replace null with anything you want..


SELECT coalesce (PARSENAME(REPLACE(_Venue,',', '.'),4), '-') address1
,coalesce(PARSENAME(REPLACE(_Venue, ',', '.'),3), '-') address2
,coalesce(PARSENAME(REPLACE(_Venue, ',', '.'),2), '-') address3
,coalesce(PARSENAME(REPLACE(_Venue, ',', '.'),1), '-') address4
FROM @t

Go to Top of Page

mflammia
Starting Member

44 Posts

Posted - 2008-06-05 : 04:56:00
That worked great thanks.

Sorry, have one more little issue with the query results I need help with, that has perhaps already been highlighted. If you take a look at the example below, the issue is that certain parts of the address are not always appearing in the same place, for example, the address name appears in a different column for each row, but I need it to always to appear in the same place so I can reference the same block and get the correct info.

Address1 Address2 Address3 Address4
Queen Elizabeth's Hunting Lodge, Rangers Road, Chingford, London E4 7 QH
- All Saints Church, Shrub End Road, Colchester
- - - Audley End House

Really appreciate the help.

Go to Top of Page

Kumar_Anil
Yak Posting Veteran

68 Posts

Posted - 2008-06-05 : 14:24:10
What I understood so far is that the address elements might be showing up in different order for every address & it might have something like
House number, street name, city, Zip etc etc or it could even be something like ... streetname, House number, street number, zip, city etc etc...
right ???
So, in the above scenario, you want to sort the address out in such a way that you want Street Number in one column, Street Name in another column, City Name in another etc etc
Im not sure as how you can do that..because it will depend a lot on the data itself & you need to identify the pattern. What I see is just series of either numbers or characters & you can use isnumeric(columnaname)=1 and see if it is numeric or not...
Go to Top of Page
   

- Advertisement -