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 |
|
pvccaz
Yak Posting Veteran
87 Posts |
Posted - 2008-05-21 : 17:01:28
|
| Hi,I have a table that has multiple postal codes in one of the columns. Those have to be split up one per line and stored in another table. The zip codes are comma seperated. Is there a function that can do this...?Example data in ZipCodeTable. (Name and ZipCode are 2 columns in a table)Name ZipCodeArea1 19930,19970,19971,19944Area2 19934,19938,19901,19903,19904Area3 19994,19838output Table should be:Name ZipCodeArea1 19930Area1 19970Area1 19971Area1 19944Area2 19934Area2 19938Area2 19901Area2 19903Area2 19904Area3 19994Area3 19838Any thoughts on this would be of much help !!..Thanks |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-05-21 : 17:17:11
|
Using Microsoft SQL Server 2000 or Microsoft SQL Server 2005? E 12°55'05.25"N 56°04'39.16" |
 |
|
|
pvccaz
Yak Posting Veteran
87 Posts |
Posted - 2008-05-21 : 17:22:08
|
| using SQL Server 2005.. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-05-21 : 17:28:57
|
[code]DECLARE @Sample TABLE (Name VARCHAR(9), ZipCodes VARCHAR(99))INSERT @SampleSELECT 'Area1', '19930,19970,19971,19944' UNION ALLSELECT 'Area2', '19934,19938,19901,19903,19904' UNION ALLSELECT 'Area3', '19994,19838'-- SQL Server 2005 ordered by listentrySELECT s.Name, p.Data AS ZipCodeFROM @Sample AS sCROSS APPLY dbo.fnParseList(',', s.ZipCodes) AS pORDER BY s.Name, p.RowID-- SQL Server 2005 ordered by ZipCodeSELECT s.Name, p.Data AS ZipCodeFROM @Sample AS sCROSS APPLY dbo.fnParseList(',', s.ZipCodes) AS pORDER BY s.Name, p.Data-- SQL Server 2000 ordered by listentrySELECT s.Name, SUBSTRING(s.ZipCodes, v.Number - 1, COALESCE(NULLIF(CHARINDEX(',', s.ZipCodes, v.Number), 0), LEN(s.ZipCodes) + 1) - v.Number + 1) AS ZipCodeFROM @Sample AS sINNER JOIN master..spt_values AS v ON v.Type = 'p'WHERE SUBSTRING(',_' + s.ZipCodes, v.Number, 1) = ','ORDER BY s.Name, v.Number-- SQL Server 2000 ordered by ZipCodeSELECT s.Name, SUBSTRING(s.ZipCodes, v.Number - 1, COALESCE(NULLIF(CHARINDEX(',', s.ZipCodes, v.Number), 0), LEN(s.ZipCodes) + 1) - v.Number + 1) AS ZipCodeFROM @Sample AS sINNER JOIN master..spt_values AS v ON v.Type = 'p'WHERE SUBSTRING(',_' + s.ZipCodes, v.Number, 1) = ','ORDER BY s.Name, 2[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
|
pvccaz
Yak Posting Veteran
87 Posts |
Posted - 2008-05-21 : 17:39:55
|
| Thank you so much... I tested the queries for sql server 2000 the output is perfect. In the sql server 2005 example i am getting an error -- Invalid object name 'dbo.fnParseList'. Where is the function available.. ?I guess i am missing something.. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-22 : 01:40:47
|
quote: Originally posted by pvccaz Thank you so much... I tested the queries for sql server 2000 the output is perfect. In the sql server 2005 example i am getting an error -- Invalid object name 'dbo.fnParseList'. Where is the function available.. ?I guess i am missing something..
Its a function available inside this forums. Look inside this forums for function code. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-05-22 : 05:48:08
|
quote: Originally posted by pvccaz Thank you so much... I tested the queries for sql server 2000 the output is perfect. In the sql server 2005 example i am getting an error -- Invalid object name 'dbo.fnParseList'. Where is the function available.. ?I guess i am missing something..
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=76033MadhivananFailing to plan is Planning to fail |
 |
|
|
pvccaz
Yak Posting Veteran
87 Posts |
Posted - 2008-05-22 : 13:05:18
|
| Thanks you visakh16 and madhivanan !!.. |
 |
|
|
|
|
|
|
|