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
 Spliting

Author  Topic 

godspeedba
Yak Posting Veteran

90 Posts

Posted - 2013-06-19 : 04:41:34
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

52326 Posts

Posted - 2013-06-19 : 04:44:39
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

90 Posts

Posted - 2013-06-19 : 05:06:58
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

52326 Posts

Posted - 2013-06-19 : 05:11:27
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

90 Posts

Posted - 2013-06-19 : 05:14:34
Visakh, once again, YOU ARE THE BEST
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-06-19 : 05:15:08
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

52326 Posts

Posted - 2013-06-19 : 05:16:55
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
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-06-19 : 05:37:50
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

52326 Posts

Posted - 2013-06-19 : 05:45:07
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
Go to Top of Page

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-06-19 : 05:49:37
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
   

- Advertisement -