加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

三个很有用的sqlserver中的处理多个以分隔符分隔的参数的函数

发布时间:2020-12-12 14:55:29 所属栏目:MsSql教程 来源:网络整理
导读:?1,获取字符串中分隔符分隔的长度 alter FUNCTION GetLenBySelChar ( ?-- Add the parameters for the function here ?@StrParams nvarchar(4000), ?@char char(1)=N'1' ) RETURNS int AS BEGIN ?declare? ?@m? int, ?@length int ?set @length=0 ? ?set? @

?1,获取字符串中分隔符分隔的长度

alter FUNCTION GetLenBySelChar
(
?-- Add the parameters for the function here

?@StrParams nvarchar(4000),
?@char char(1)=N'1'
)
RETURNS int
AS
BEGIN
?declare? ?@m? int,

?@length int
?set @length=0
?
?set? @m=CHARINDEX(@char,@strparams)?

?WHILE? @m>0?
?BEGIN?
???? set? @strparams=substring(@strparams,@m+1,datalength(@strparams)-1)?
???set @length=@length+1
???? set? @m=CHARINDEX(@char,@strparams)?
?END
?return @length


END

2,返回每个分隔的字符串

ALTER FUNCTION [dbo].[funGetSubStrBySelChar]
(
?-- Add the parameters for the function here

?@StrParams nvarchar(4000),
?@char char(1)=N'1'
)
RETURNS nvarchar(4000)
AS
BEGIN
?declare? --@str? varchar(300),
?@id? varchar(3000),
?@m? int,
?@n? int?
?--set? @str='a,b,c,d,e'?
?--set? @m=CHARINDEX(',',@str)?
?--set? @n=1?
?--WHILE? @m>0?
?--BEGIN?
?--??? set? @id=substring(@str,@n,@m-@n)?
?--??? print? @id?
?--??? set? @n=@m+1?
?--??? set? @m=CHARINDEX(',@str,@n)?
?--END
?set @m=charindex(@char,@strparams)
?set @n=1
?if @m>0
?begin
??set @id=substring(@strparams,1,@m-@n)

?end
?else
?begin
??set @id=null
?end
???return @id

END

三,返回取出分隔符后新的字符串

ALTER FUNCTION [dbo].[funGetNewStr]
(
?-- Add the parameters for the function here

?@StrParams nvarchar(4000),
?@char char(1)=N','
)
RETURNS nvarchar(4000)
AS
BEGIN
?declare? --@str? varchar(300),@strparams)
?if @m>0
?begin
?set @strparams=substring(@strparams,datalength(@strparams)-@m-1)
?end
?return @strparams

END

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读