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
 Split field

Author  Topic 

SukhCRM
Starting Member

2 Posts

Posted - 2010-07-13 : 07:26:20
Hi Guys

Hope 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 ADDRESS1

However 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 ADDRESS1

remove the part marked in red & try.


Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless.

PBUH
Go to Top of Page

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.
Go to Top of Page

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 like

SELECT 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
Go to Top of Page

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.
Go to Top of Page

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 like

SELECT 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 here

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=76033#315325


Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless.

PBUH
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-07-13 : 08:17:21
corrected version:

drop function fnParseArray
CREATE FUNCTION dbo.fnParseArray (@array VARCHAR(1000),@separator VARCHAR(10))
RETURNS @T Table (occ int,col1 varchar(50))
AS
BEGIN
DECLARE @separator_position INT
DECLARE @array_value VARCHAR(1000)
declare @occurence int =1 -- to get the Nth occurence
if (left(@array,len(@separator))=@separator)
begin
set @array=stuff(@array,1,len(@separator),'')
end
if (right(@array,len(@separator))<>@separator)
begin
SET @array = @array + @separator
end
WHILE 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, '')
END
RETURN
END

-- Example to call
select * from
dbo.fnParseArray ('12 test street<br>knighton<br>london<br>sw3 4rg<br>United Kingdom','<br>')
order by occ desc

-- or
select * 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.
Go to Top of Page
   

- Advertisement -