Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12

SQL Function to Split Comma Separated Values and Insert into Table Expand / Collapse
Author
Message
Posted Tuesday, November 30, 2010 9:29 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Sunday, September 29, 2013 1:24 AM
Points: 429, Visits: 1,721
I've been using Jeff Moden's procedures and a tally table for years. The function in the OP is not the correct way to split CSVs, with the possible exception of a quick-and-dirty one time application for someone in a hurry who has never used CTEs before. But it's not that hard and if you've had to do it once, it's likely you'll have to do it again sometime.
 
Post #1028062
Posted Tuesday, November 30, 2010 9:32 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, October 13, 2014 8:06 AM
Points: 31, Visits: 333
Just switched to WayneS' non-RBAR function. Now that's FAST!
Post #1028064
Posted Tuesday, November 30, 2010 10:12 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:25 AM
Points: 13,238, Visits: 11,010
WayneS (11/30/2010)
The latest (and fastest) Delimited String Splitter Function can be found here.


Thanks for the link!




How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1028098
Posted Wednesday, December 1, 2010 6:51 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Friday, October 17, 2014 7:46 AM
Points: 5,364, Visits: 8,948
chris-860960 (11/30/2010)
Just switched to WayneS' non-RBAR function. Now that's FAST!

It's not "my" function - it comes from Jeff Moden. It's just what is in use around here, with little tweaks here and there to make it as fast as it possible can be.


Wayne
Microsoft Certified Master: SQL Server 2008
If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
Links: For better assistance in answering your questions, How to ask a question, Performance Problems, Common date/time routines,
CROSS-TABS and PIVOT tables Part 1 & Part 2, Using APPLY Part 1 & Part 2, Splitting Delimited Strings
Post #1028604
Posted Friday, December 3, 2010 5:35 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, November 30, 2011 4:32 PM
Points: 16, Visits: 158
Hello,

Would you happen to have any suggestions on how to do the opposite, take a result set and create one line csv file. Example 20 lines of data are returned in a result set displaying the total # of Available hospital beds. How can I get the 20 lines of data on one csv line. any help or assistance would be greatly appreciated.
Post #1029792
Posted Friday, December 3, 2010 5:57 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, October 13, 2014 8:06 AM
Points: 31, Visits: 333
TurnerC,

Someone will probably point out a much better way of doing this but getting a single column of multiple records into a comma-separated string can be done thusly:

SELECT STUFF((SELECT ',' + CAST(TableName.NumBeds AS varchar) FROM TableName FOR XML PATH('')),1, 2, '') AS CSVColumn

You have to CAST any output fields as strings (as long as they aren't already, I'm presuming your Number of Beds field would be an integer) for it to work.
Post #1029800
Posted Friday, December 3, 2010 6:08 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, November 30, 2011 4:32 PM
Points: 16, Visits: 158
Thank you for your reply. I will try it out.

Would you happen to know if there are performance issues with using Coalesce. I read an article on how to creat a csv file using Coalesce. I am just unsure about the performance side.

Thank you
Post #1029806
Posted Friday, December 3, 2010 6:40 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, October 13, 2014 8:06 AM
Points: 31, Visits: 333
Would the COALESCE be this sort of query?:

DECLARE @EmployeeList varchar(100)
SELECT @EmployeeList = COALESCE(@EmployeeList + ', ', '') + CAST(Emp_UniqueID AS varchar(5))
FROM SalesCallsEmployees
WHERE SalCal_UniqueID = 1
SELECT @EmployeeList

In my experience COALESCE can be a bit of a performance hit. Certainly the above on one of my database columns (varchar(255)) with 22,000+ records the above is about 3-4 times slower than the STUFF...XML method.
Post #1029829
Posted Friday, December 3, 2010 8:28 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 8:45 PM
Points: 35,342, Visits: 31,875
WayneS (12/1/2010)
chris-860960 (11/30/2010)
Just switched to WayneS' non-RBAR function. Now that's FAST!

It's not "my" function - it comes from Jeff Moden. It's just what is in use around here, with little tweaks here and there to make it as fast as it possible can be.


Thanks for the kudo but it's not my original idea, either. It's an assembly of good practice from many authors with many lessons learned. Many others have have put together similar functions.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1030220
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse