sql server - Split String With Multiple Delimiters and Retain Delimiters -


i need split string rows @ specific word boundaries. catch need maintain specific word boundary triggered split because later, want recombine rows , see delimiter. i'm updating existing function looks this:

declare @input varchar(8000) = 'ac/dc, quick, brown fox' declare @delimiters varchar(100) = '%[ ,-/]%'  ;with      [elements]     (         select             1 position             , 1 startoffset             , patindex(@delimiters, @input) - 1 endoffset             , @input input             , substring(@input, 1, isnull(nullif(patindex(@delimiters, @input), 0) - 1, 8000)) word         union         select              position + 1 position             , endoffset + 2 startoffset             , endoffset + isnull(nullif(patindex(@delimiters, substring(input, endoffset + 2, 8000)), 0), len(@input) - endoffset) endoffset             , input             , substring(input, endoffset + 2, isnull(nullif(patindex(@delimiters, substring(input, endoffset + 2, 8000)), 0) - 1, 8000)) word                      [elements]                      endoffset between 1 , len(@input) - 1     ) select     *     [elements] 

giving me:

+----------+-------------+-----------+-----------------------------+-------+ | position | startoffset | endoffset |            input            | word  | +----------+-------------+-----------+-----------------------------+-------+ |        1 |           1 |         2 | ac/dc, quick, brown fox | ac    | |        2 |           4 |         5 | ac/dc, quick, brown fox | dc    | |        3 |           7 |         6 | ac/dc, quick, brown fox |       | |        4 |           8 |        10 | ac/dc, quick, brown fox |   | |        5 |          12 |        16 | ac/dc, quick, brown fox | quick | |        6 |          18 |        17 | ac/dc, quick, brown fox |       | |        7 |          19 |        23 | ac/dc, quick, brown fox | brown | |        8 |          25 |        27 | ac/dc, quick, brown fox | fox   | +----------+-------------+-----------+-----------------------------+-------+ 

this breaks nicely omits "/" , "," rows result set. have numbers table can bump against , i'm pretty flexible how can accomplished.

i brute force way through loop seems barbaric.

well, close, have amend logic when "split" on delimiter or not. code gets ugly, don't have brute force it. if worked on it, might able make "prettier". building off you'd created.

declare @input varchar(8000) = ',ac/dc,,the quick, brown fox-hound' declare @delimiters varchar(100) = '%[ ,-/]%'  ;with      [elements]     (         select             1 position             , 1 startoffset             , patindex(@delimiters, @input) - 1 + case when  patindex(@delimiters, left(@input,1)) = 1 1 else 0 end endoffset             , @input input             , substring(@input, 1, isnull(nullif(patindex(@delimiters, @input), 0) - 1 +case when  patindex(@delimiters, left(@input,1)) = 1 1 else 0 end, 8000)) word         union         select              position +1 position             , endoffset + 1 startoffset             , endoffset + isnull(nullif(patindex(@delimiters, substring(input, endoffset+1 , 8000)), 0) -  case when  patindex(@delimiters, substring(input, endoffset+1 , 8000)) = 1 0 else 1 end  ,datalength(input)-endoffset) endoffset             , input             , substring(input, endoffset+ 1 , isnull(nullif(patindex(@delimiters, substring(input, endoffset+1 , 8000)), 0) -  case when  patindex(@delimiters, substring(input, endoffset+1 , 8000)) = 1 0 else 1 end  ,datalength(input)-endoffset)) word                      [elements]                      endoffset between 1 , datalength(@input)-1      ) select     *     [elements] 

Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -