sql server 获取分隔字符串后的长度
--方法1
--sql 分隔字符串,返回个数
CREATE
function
f_splitLen_1
(
@str
varchar
(1024),
--要分割的字符串
@split
varchar
(10)
--分隔符号
)
returns
int
as
begin
declare
@location
int
declare
@start
int
declare
@length
int
set
@str=ltrim(rtrim(@str))
set
@location=charindex(@split,@str)
set
@length=1
while @location<>0
begin
set
@start=@location+1
set
@location=charindex(@split,@str,@start)
set
@length=@length+1
end
return
@length
end
--演示1
select
dbo.f_splitLen_1(
'1,2,3,4'
,
','
)
as
'Count'
-- 查询结果为:4
create
function
f_splitLen_2
(
@str
varchar
(1024),
--要分割的字符串
@split
varchar
(10)
--分隔符号
)
returns
int
as
begin
return
len(@str)-len(
replace
(@str,@split,
''
))+1
end
--演示2
select
dbo.f_splitLen_2(
'1,2,3,4'
,
','
)
as
'Count'
-- 查询结果为:4