我需要为带有两个字符的拆分字符串编写函数。例如:“海得拉巴 Hyd,测试”
在上面的字符串中,我需要用空格(“”)和逗号(,)吐出,输出结果将保存在表格中
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
)
请帮忙......
提前致谢..