MS Sql 一些 技巧
1、使用指定的字符串分割,返回分割后元素的个数
create function Get_StrLength
(
@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
调用方法:select dbo.Get_StrLength('7,5,6,7,a,f,d',',')
create function Get_StrLength
(
@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
调用方法:select dbo.Get_StrLength('7,5,6,7,a,f,d',',')