|
The databases I look after are still back in the stone age on SQL 2000, but I'm curious to know how Splitting a delimited string has improved since SQL2000.
(comments are locked)
|
|
The SQL 2008 (and I think 2005) samples contain code for CLR functions that can split and concatenate strings. (the concatenate function is an aggregate, and it's very handy). I can't find a URL but it should be in BOL under "Samples". There's also my favorite: http://www.sqlteam.com/article/parsing-csv-values-into-multiple-rows :)
(comments are locked)
|
|
I have found the best way is to simply write a UDF that performs the split and returns a TABLE with the results. Now, that has been around since SQL 2000, so no difference there. However, since 2005, we can now use CROSS APPLY (and OUTER APPLY), meaning we can "join" to the multi-row results returned from these UDF's to quickly take a table of CSV strings and return them all broken out into individual rows. That, to me, is still the quickest, shortest and most efficient way to handle things. In other words, something like this:
Indeed. I have separate UDFs that return INT or VARCHAR resultsets so that the subsequent JOIN is datatype-matched
Oct 07 '09 at 05:14 PM
Kristen ♦
(comments are locked)
|
|
Other than SQLCLR there's no really efficient way to handle this is SQL Server 2005+. It is still best to handle the string breaking at the client side and pass the broken pieces to the database. How would you "pass the broken pieces to the database"? Lets say I want to match a product code against any in the list "ABC123,DEF456,GHI789" (currently I would pass that string to my Sproc and SPLIT it to a temporary table there, and then use that temporary table in a JOIN)
Oct 07 '09 at 03:26 PM
Kristen ♦
In SQL 2008 you could use a table input parameter and pass the list into the procedure as a recordset then use a simple subquery lookup WHERE ProductCode IN (SELECT ProductCode FROM YourInputParameter). Or pass in a XML document with multiple values. Takes some extra CPU power, but you don't have to do any funky text manipulation in the proc.
Oct 07 '09 at 04:09 PM
mrdenny
(comments are locked)
|

