SQL Server2005截取字符串并操作

来源:互联网 发布:linux怎么配置ip地址 编辑:程序博客网 时间:2024/05/11 17:48
--定义须查询的字串declare @queryString nvarchar(1000)--定义循环内截取出来的子字符串,如有字符串'abc,123,中国,',循环内第一次截取@temp='abd',第二次@temp='123',第三次@temp='中国'declare @temp varchar(32)--定义分隔符','的位置declare @pos int--定义截取出来的子字符串的长度declare @len intset @queryString='abc,123,中国,'--Charindex(分隔符,被查询的字符串,查询起始位置),这里是取分隔符','出现的位置set @pos=Charindex(',',@queryString,0)while(@pos>0)begin--Substring(被查询的字符串,起始位置,截止位置),根据分隔符的位置截取字符串set @temp=Substring(@queryString,0,@pos)--len(子字符串),取得子字符串的长度set @len=len(@temp)--下面只是把分隔符之间的字符串依次输出,可替换成你自己的操作select @temp--Stuff(被查询的字符串,起始位置,截止位置),把子字符串从被查询的字符串中去除,第一次循环时把'abc,'从'abc,123,中国,'去除,这时queryString就是'123,中国,'了set @queryString=Stuff(@queryString,1,@len+1,'')--下面是控制循环的条件set @pos=Charindex(',',@queryString,0)end



原创粉丝点击