Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Nested Split WITHOUT Cross APPLY

$
0
0

I have been wrestling with this for days and need an assist.

I have to split a text field based on different Delimitters AND I need to do
it without CROSS Apply because the database I am working in has a compatability
level of less than 90.

This is what I have attempted with the cross apply and it ALMOST works.

Again the issue is that

I want this

ID Company  AdjustedZip
1 McDonalds 55407
2 Burger
King 55401
3 Wendys  32314
3 Wendys  32121
4 ChikFilA 30317
4 ChikFilA 30318
4 ChikFilA 30318
5 Checkers 90134
5 Checkers 90301
5 Checkers 90210
5 Checkers 90211

And I am getting this

ID Company  AdjustedZip
1 McDonalds 55407
2 Burger
King 55401
3 Wendys  32314
3 Wendys  3,231,432,121
3 Wendys  32121
4 ChikFilA 30317
4 ChikFilA 303,173,031,830,318
4 ChikFilA 30318
4 ChikFilA 30318
5 Checkers 90134
5 Checkers 90134;90301;90210;90211
5 Checkers 90301
5 Checkers 90210
5 Checkers 90211

BUT I CAN'T use the Cross Apply to do it,

Here is my code

Create FUNCTION [dbo].[DelimitedSplitMax]
--===== Define I/O
parameters
        (@pString VARCHAR(max), @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
       E16(N) AS (SELECT 1 FROM E4 a, E4 b),
--10E+16 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) = @pDelimiter
                ),
cteLen(N1,L1)
AS(--==== Return start and length (for use in substring)
                
SELECT s.N1,
                       
ISNULL(NULLIF(CHARINDEX(@pDelimiter,@pString,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

Create Table #Temp
(Id int
,Company varchar (100)
,AdjustedZip
varchar(255)
)

Insert #Temp
(Id
,Company
,AdjustedZip
)
Values

  (1,'McDonalds','55407')
       ,(2,'Burger King','55401')
      
,(3,'Wendys','32314,32121')
      
,(4,'ChikFilA','30317,30318,30319')
        
,(5,'Checkers','90134;90301;90210;90211')
 Select s.ID
  ,s.Company     

        ,Case
   When Len(Ltrim(Rtrim(s.adjustedzip)))= 5 Then
s.adjustedzip
   Else
   f.Item
   End  
   as
AdjustedZip
         From
  #temp as s
CROSS APPLY
(
Select *
From dbo.DelimitedSplitMax(ltrim(rtrim(s.AdjustedZip)), ',')
Union 

Select * From dbo.DelimitedSplitMax(ltrim(rtrim(s.AdjustedZip)), ';') 

 ) as f 



Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>