sql:字符串按照某个字符分割后取第几个字符串
时间:2011-03-31 来源:kuailewangzi1212
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
ALTER function uf_getStrBySplit(@old_str varchar(8000),@split varchar(50),@pos int) returns varchar(200)
as
/*
功能描述:返回分割后的字符串的第几个字符串
参数说明:@old_str:原字符串
@split:分隔符
@pos:第几个
*/
begin
declare @rtn varchar(200),
@li_p int
select @rtn=''
select @old_str=ltrim(rtrim(@old_str))+@split
select @li_p=0
while charindex(@split,@old_str)>1
begin
select @li_p=@li_p + 1
if @li_p=@pos
begin
select @rtn=left(@old_str,charindex(@split,@old_str) - 1)
return @rtn
end
select @old_str=right(@old_str,len(@old_str) - charindex(@split,@old_str))
end
return @rtn
end
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
相关阅读 更多 +