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.
| Author |
Topic |
|
SukhCRM
Starting Member
2 Posts |
Posted - 2010-07-13 : 07:26:20
|
| Hi GuysHope someone can help. I have an address field which I need to split into multiple columns. The address field contains data in the following format.12 test street<br>knighton<br>london<br>sw3 4rg<br>United Kingdom. I have tried to use the PARSENAME function to split the fields as shown below.select PARSENAME(REPLACE(ShippingAddress,'<br>','.'),1) as ADDRESS1However the field value that is returned is null.Can someone help.Thanks |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-07-13 : 07:33:30
|
Parsename will not work for delimiters more than 4.select PARSENAME(REPLACE('12 test street<br>knighton<br>london<br>sw3 4rg<br>United Kingdom','<br>','.'),1) as ADDRESS1remove the part marked in red & try.Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless. PBUH |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-07-13 : 07:34:14
|
PARSENAME will not work for a five-part-name. No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
SukhCRM
Starting Member
2 Posts |
Posted - 2010-07-13 : 07:37:25
|
| Thanks guys for the reply. If PARSENAME only works up to 4 delimeters, what is the best way for me to achieve this result? I'm thinking I could do something likeSELECT LEFT(ShippingAddress,CHARINDEX('<br>',ShippingAddress)-1) AS ADDRESS1,LEFT(SUBSTRING(ShippingAddress,(CHARINDEX('<br>',ShippingAddress)+4),LEN(ShippingAddress)),CHARINDEX('<br>',SUBSTRING(ShippingAddress,(CHARINDEX('<br>',ShippingAddress)+4),LEN(ShippingAddress)))-1) AS ADDRESS2 |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-07-13 : 07:40:01
|
My provided solution has a bug hence I deleted it.I will come back... No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-07-13 : 07:51:22
|
quote: Originally posted by SukhCRM Thanks guys for the reply. If PARSENAME only works up to 4 delimeters, what is the best way for me to achieve this result? I'm thinking I could do something likeSELECT LEFT(ShippingAddress,CHARINDEX('<br>',ShippingAddress)-1) AS ADDRESS1,LEFT(SUBSTRING(ShippingAddress,(CHARINDEX('<br>',ShippingAddress)+4),LEN(ShippingAddress)),CHARINDEX('<br>',SUBSTRING(ShippingAddress,(CHARINDEX('<br>',ShippingAddress)+4),LEN(ShippingAddress)))-1) AS ADDRESS2
Have a look herehttp://www.sqlteam.com/forums/topic.asp?TOPIC_ID=76033#315325Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless. PBUH |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-07-13 : 08:17:21
|
corrected version:drop function fnParseArrayCREATE FUNCTION dbo.fnParseArray (@array VARCHAR(1000),@separator VARCHAR(10))RETURNS @T Table (occ int,col1 varchar(50))AS BEGINDECLARE @separator_position INT DECLARE @array_value VARCHAR(1000)declare @occurence int =1 -- to get the Nth occurenceif (left(@array,len(@separator))=@separator)begin set @array=stuff(@array,1,len(@separator),'')endif (right(@array,len(@separator))<>@separator)begin SET @array = @array + @separatorendWHILE PATINDEX('%' + @separator + '%', @array) <> 0 BEGIN SELECT @separator_position = PATINDEX('%' + @separator + '%',@array) SELECT @array_value = LEFT(@array, @separator_position - 1) INSERT into @T VALUES (@occurence,@array_value) set @occurence = @occurence + 1 SELECT @array = STUFF(@array,1, @separator_position + len(@separator)-1, '')ENDRETURN END-- Example to callselect * from dbo.fnParseArray ('12 test street<br>knighton<br>london<br>sw3 4rg<br>United Kingdom','<br>')order by occ desc-- orselect * from dbo.fnParseArray ('12 test street<br>knighton<br>london<br>sw3 4rg<br>United Kingdom','<br>')where occ=1 No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
|
|
|
|
|