|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Friday, February 22, 2013 11:34 AM
Points: 7,
Visits: 18
|
|
| Tested function with 2 delimiters, works great, although it takes some time to run (20 minutes to run on a table with 887,994 rows whose splitting resulted in 13,397,215 rows).
|
|
|
|
|
SSCrazy Eights
        
Group: General Forum Members
Last Login: Today @ 3:32 PM
Points: 8,980,
Visits: 8,540
|
|
ahpitre (2/13/2013) How do you use this function? Also, how can I pass an additional parameter, so it's always inserted into the new table? I have a column named Part. I want the table with the split to include Part (which is repeated for all substrings that are splitted from main string). My final output should be something like this :
Input_table
Part Specs --------------------------------- 123 D-dfldkk; P-4987843; D48974587 456 A-dfldkk; Z-4987843
Output_table (created by Split function)
Part Specs --------------------------------- 123 D-dfldkk 123 P-4987843 123 D48974587 456 A-dfldkk 456 Z-4987843
By using cross apply. I will show you the code but please PLEASE PLEASE do NOT just blindly use this code. You need to understand what it is doing so you are able to support it. It is your phone that will be ringing at 3am not mine. 
if object_id('tempdb..#Input') is not null drop table #Input create table #Input ( Part int, Specs varchar(50) )
insert #Input select 123, 'D-dfldkk; P-4987843; D48974587' union all select 456, 'A-dfldkk; Z-4987843'
--The above is your table
--To avoid the performance issues with using replace while calling the DelimitedSplit8K function --we can do this with a cte
;with cte as ( select Part, Replace(Specs, '; ', ';') as Specs from #Input )
--Now we just need to retrieve the data select cte.Part, s.Item as Specs from cte cross apply dbo.DelimitedSplit8k(cte.Specs, ';') s
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Moden's splitter.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Friday, February 22, 2013 11:34 AM
Points: 7,
Visits: 18
|
|
THis function works great, but, has the following limitations or things that can be improved :
1) If the string to be splitted has the delimiter character at the end, then, it returns an additional item/ItemNumber row where item will be empty. For example :
Searching for ; on a string that contains 'ddkfjdkdjkdfkdjl;1111;' will result in :
ItemNumber Item ------------------------------------------ 1 ddkfjdkdjkdfkdjl 2 1111 3
So, function needs to be fixed to not return the last item if it will result in an empty item (NULL or '').
2) I managed to put all my delimiters on a table, then, CROSS Join my main table to the Delimiters table. The result allows you to dynamically insert the character being searched for, and, searching for multiple characters without having to hard code them on the SQL command. An example of my 1st step prior to using the delimiter8k function :
SELECT * FROM dbo.tblDelimiters CROSS JOIN dbo.qryDoc_Ref_Specs_to_Doc_Controlling_Parts WHERE (dbo.qryDoc_Ref_Specs_to_Doc_Controlling_Parts.[Reference Specs] LIKE N'%' + dbo.tblDelimiters.delimiter)
I then use the above results as my data source for the CROS APPLY query with the delimited8K function. My query then looks like this :
SELECT DISTINCT * FROM [ESM].[dbo].[qryDoc_Ref_Specs_to_Doc_Controlling_Parts_delimited_Ref_Specs] CROSS APPLY dbo.DelimitedSplit8K([Reference Specs], [Reference Specs delimiter])
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 7:18 AM
Points: 33,112,
Visits: 27,038
|
|
ahpitre (2/18/2013) THis function works great, but, has the following limitations or things that can be improved :
1) If the string to be splitted has the delimiter character at the end, then, it returns an additional item/ItemNumber row where item will be empty. For example :
Searching for ; on a string that contains 'ddkfjdkdjkdfkdjl;1111;' will result in :
ItemNumber Item ------------------------------------------ 1 ddkfjdkdjkdfkdjl 2 1111 3
So, function needs to be fixed to not return the last item if it will result in an empty item (NULL or '').
2) I managed to put all my delimiters on a table, then, CROSS Join my main table to the Delimiters table. The result allows you to dynamically insert the character being searched for, and, searching for multiple characters without having to hard code them on the SQL command. An example of my 1st step prior to using the delimiter8k function :
SELECT * FROM dbo.tblDelimiters CROSS JOIN dbo.qryDoc_Ref_Specs_to_Doc_Controlling_Parts WHERE (dbo.qryDoc_Ref_Specs_to_Doc_Controlling_Parts.[Reference Specs] LIKE N'%' + dbo.tblDelimiters.delimiter)
I then use the above results as my data source for the CROS APPLY query with the delimited8K function. My query then looks like this :
SELECT DISTINCT * FROM [ESM].[dbo].[qryDoc_Ref_Specs_to_Doc_Controlling_Parts_delimited_Ref_Specs] CROSS APPLY dbo.DelimitedSplit8K([Reference Specs], [Reference Specs delimiter])
Delimiter at the end means the same thing as a delimiter at the beginning. There's a missing element. Don't fix it in the splitter. Fix it in the code that uses the delimiter output.
--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."
For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Today @ 6:22 PM
Points: 1,336,
Visits: 4,023
|
|
Hi Jeff,
I just sent you an email with some interesting performance test results if you get a chance to have a look.
MM
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: Today @ 3:22 PM
Points: 3,678,
Visits: 5,177
|
|
mister.magoo (2/21/2013)
Hi Jeff, I just sent you an email with some interesting performance test results if you get a chance to have a look. 
Hey, NOT COOL!! We do PUBLIC performance testing around these parts! You test it, you post it!
Best,
Kevin G. Boles SQL Server Consultant SQL MVP 2007-2012 TheSQLGuru at GMail
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Today @ 6:22 PM
Points: 1,336,
Visits: 4,023
|
|
TheSQLGuru (2/22/2013)
Hey, NOT COOL!! We do PUBLIC performance testing around these parts! You test it, you post it! 
Right, if I get flamed for this, you have to save me !
I have crudely highlighted the two winners...the CLR split clearly still way ahead, but the new T-SQL version not too shabby

Attached testing results data spreadsheet for those that trust me enough to open it (it is safe !)
And here is the code (with notes removed for brevity) and the changes underlined.
CREATE FUNCTION [dbo].[DelimitedSplit8KB] --===== Define I/O parameters (@pString VARCHAR(8000) , @pDelimiter CHAR(1)) --WARNING!!! DO NOT USE MAX DATA-TYPES HERE! IT WILL KILL PERFORMANCE! RETURNS TABLE WITH SCHEMABINDING AS RETURN --===== "Inline" CTE Driven "Tally Table" produces values from 1 up to 10,000... -- enough to cover VARCHAR(8000) WITH E1(N) AS ( SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 ), --10E+1 or 10 rows E2(N) AS (SELECT 1 FROM E1 a, E1 b), --10E+2 or 100 rows E4(N) AS (SELECT 1 FROM E2 a, E2 b), --10E+4 or 10,000 rows max cteTally(N) AS (--==== This provides the "base" CTE and limits the number of rows right up front -- for both a performance gain and prevention of accidental "overruns" SELECT TOP (ISNULL(DATALENGTH(@pString),0)) ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E4 ), cteStart(N1) AS (--==== This returns N+1 (starting position of each "element" just once for each delimiter) SELECT 1 UNION ALL SELECT t.N+1 FROM cteTally t WHERE SUBSTRING(@pString,t.N,1) COLLATE Latin1_General_BIN = @pDelimiter COLLATE Latin1_General_BIN ), cteLen(N1,L1) AS(--==== Return start and length (for use in substring) SELECT s.N1, ISNULL(NULLIF(CHARINDEX(@pDelimiter COLLATE Latin1_General_BIN,@pString COLLATE Latin1_General_BIN,s.N1) ,0)-s.N1,8000) FROM cteStart s ) --===== Do the actual split. The ISNULL/NULLIF combo handles the length for the final element when no delimiter is found. SELECT ItemNumber = ROW_NUMBER() OVER(ORDER BY l.N1), Item = SUBSTRING(@pString, l.N1, l.L1) FROM cteLen l ;
Sorry if I have made a mistake, but I think this is valid.
MM
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 12:10 PM
Points: 7,185,
Visits: 7,285
|
|
mister.magoo (2/22/2013) And here is the code (with notes removed for brevity) and the changes underlined. Sorry if I have made a mistake, but I think this is valid. Looks good to me - using a binary collation probably speeds up the comparison.
Something completely different though: the three comments --10E+1 or 10 rows --10E+2 or 100 rows --10E+4 or 10,000 rows max are all wrong: "10E" should be "1E" in each case.
Tom Is minic a gheibheann béal oscailte dorn dúnta. Is minig a cheapas beul fosgailte dòrn dùinte.
http://es.linkedin.com/in/tomthomsonsoftware
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 8:57 AM
Points: 5,705,
Visits: 11,140
|
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Today @ 6:22 PM
Points: 1,336,
Visits: 4,023
|
|
ChrisM@Work (2/22/2013)
L' Eomot Inversé (2/22/2013)
mister.magoo (2/22/2013) And here is the code (with notes removed for brevity) and the changes underlined. Sorry if I have made a mistake, but I think this is valid.Looks good to me - using a binary collation probably speeds up the comparison.... Confirmed. A process I'm currently working on which makes extensive use of the function, runs in less than half the time using MM's modification.
Nice to hear confirmation from a real example.
What bothered me was that feeling that "someone should have thought of this ages ago, shouldn't they"? Always makes me nervous when that is the case...
MM
|
|
|
|