I need to write function for split string with two chars.
Ex:“Hyderabad Hyd,Test”
In the above string i need to spit with space(” “) and comma(,) and the out result will keep in a table
The oputput should be:
Hyderabad
Hyd,Test
Hyd
Test
CREATE function dbo.SplitString
(
@str nvarchar(4000),
@separator char(1)
)
returns table
AS
return (
with tokens(p, a, b) AS (
select
1,
1,
charindex(@separator, @str)
union all
select
p + 1,
b + 1,
charindex(@separator, @str, b + 1)
from tokens
where b > 0
)
select
p-1 SNO,
substring(
@str,
a,
case when b > 0 then b-a ELSE 4000 end)
AS word
from tokens
)
Plz do help…..
Thanks in advance..
For the results you showed, you don’t need a new split function. Just a normal one that takes a list and a separator.
The first_split will be
HyderabadandHyd,Test.The second split will be…
–
HyderabadUNIONHyderabadwhich is justHyderabad– Plus
Hyd,TestUNIONHydandTestGiving…
HyderabadHyd,TestHydTest