SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Spliting
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

godspeedba
Yak Posting Veteran

89 Posts

Posted - 06/19/2013 :  04:41:34  Show Profile  Reply with Quote
I have an old system where the address in its entirety is stored in one field.

I need to develop a solution where I need to split the field into subsequent fields:

address

split to
address1
address2
town
county

The address solution is comma separated, but I have never used any split functions in SQL, do they even exist?

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 06/19/2013 :  04:44:39  Show Profile  Reply with Quote
yep they exists
see a few in below links

http://www.sqlservercentral.com/articles/Tally+Table/72993/

http://visakhm.blogspot.com/2010/02/parsing-delimited-string.html

http://visakhm.blogspot.com/2013/01/delimited-string-split-xml-parsing.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

godspeedba
Yak Posting Veteran

89 Posts

Posted - 06/19/2013 :  05:06:58  Show Profile  Reply with Quote
Thanks for this, how would I go about putting them in the fields. As far as I can see there no way of referencing the split words directly.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 06/19/2013 :  05:11:27  Show Profile  Reply with Quote
there are ways

for example something like

SELECT t.Address,
MAX(CASE WHEN ID=1 THEN Val END) AS [Address1],
MAX(CASE WHEN ID=2 THEN Val END) AS [Address2],
MAX(CASE WHEN ID=3 THEN Val END) AS [Town],
MAX(CASE WHEN ID=4 THEN Val END) AS [county]
FROM YourTable t
CROSS APPLY dbo.ParseValues(t.Address,',')f
GROUP BY t.Address


provided the format in address field data is consistent

ParseValues definition is here

http://visakhm.blogspot.com/2010/02/parsing-delimited-string.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

godspeedba
Yak Posting Veteran

89 Posts

Posted - 06/19/2013 :  05:14:34  Show Profile  Reply with Quote
Visakh, once again, YOU ARE THE BEST
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 06/19/2013 :  05:15:08  Show Profile  Reply with Quote
for putting it into fields use

UPDATE t
SET t.Address1 = t1.Address1,
t.Address2 = t1.Address2,
t.Town = t1.Town,
t.county = t1.county
FROM table t
CROSS APPLY (SELECT MAX(CASE WHEN ID=1 THEN Val END) AS [Address1],
MAX(CASE WHEN ID=2 THEN Val END) AS [Address2],
MAX(CASE WHEN ID=3 THEN Val END) AS [Town],
MAX(CASE WHEN ID=4 THEN Val END) AS [county]
FROM dbo.ParseValues(t.Address,',')
)t1


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 06/19/2013 :  05:16:55  Show Profile  Reply with Quote
quote:
Originally posted by godspeedba

Visakh, once again, YOU ARE THE BEST


you're welcome
I find myself as a noble follower of "real experts" here

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2170 Posts

Posted - 06/19/2013 :  05:37:50  Show Profile  Reply with Quote
If you doesn't have any . (dot symbol) in the address field, then you can use this approach
SELECT
PARSENAME( REPLACE( Address, ',', '.'), 1) [Address1]
,PARSENAME( REPLACE( Address, ',', '.'), 2) [Address2]
,PARSENAME( REPLACE( Address, ',', '.'), 3) [Town]
,PARSENAME( REPLACE( Address, ',', '.'), 4) [Country]
FROM TableName



--
Chandu
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 06/19/2013 :  05:45:07  Show Profile  Reply with Quote
quote:
Originally posted by bandi

If you doesn't have any . (dot symbol) in the address field, then you can use this approach
SELECT
PARSENAME( REPLACE( Address, ',', '.'), 1) [Address1]
,PARSENAME( REPLACE( Address, ',', '.'), 2) [Address2]
,PARSENAME( REPLACE( Address, ',', '.'), 3) [Town]
,PARSENAME( REPLACE( Address, ',', '.'), 4) [Country]
FROM TableName



--
Chandu


this will only work if it has a maximum of 3 commas (ie total of 4 value components). if it has more parts this will fail which is why i dont prefer this
Also if there are existing . characters within address like st., jn. etc that will also cause result to skew
------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs

Edited by - visakh16 on 06/19/2013 05:46:05
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2170 Posts

Posted - 06/19/2013 :  05:49:37  Show Profile  Reply with Quote
quote:
Originally posted by visakh16
Also if there are existing . characters within address like st., jn. etc that will also cause result to skew


I too agree visakh... that's why I kept that limitation also...

--
Chandu
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.11 seconds. Powered By: Snitz Forums 2000