电脑安全

您当前的位置:主页 > 电脑安全 >

sql字符串分割split函数-网络编程

来源:[db:来源]标题:   字符串    编辑:电脑技术全能网 时间:2019-09-19 18:30

原标题:sql字符串分割split函数-网络编程
   sql字符串宰割split函数应用指定的字符宰割字符串。思绪:1,先盘算出指定的宰割字符在字符串中的索引,2.而后再宰割字符串。sql代码以下:declare@strordernvarchar(200)declare@ismulorderintdeclare@firstOrdernvarchar(200)declare@secondOrdernvarchar(200)set@strorder='LastModifyDateDesc,OurPriceAsc'selectCHARINDEX(',',@strorder,0)--selectLEN(@strorder)--selectSUBSTRING(@strorder,0,CHARINDEX(',',@strorder,0))--selectSUBSTRING(@strorder,CHARINDEX(',',@strorder,0)+1,LEN(@strorder))set@ismulorder=CHARINDEX(',',@strorder,0)if@ismulorder>0beginset@firstOrder='p.'+SUBSTRING(@strorder,0,CHARINDEX(',',@strorder,0))set@secondOrder='p.'+SUBSTRING(@strorder,CHARINDEX(',',@strorder,0)+1,LEN(@strorder))set@strorder=@firstOrder+','+@secondOrderendselect@strorder应用split函数create function [dbo].[split](@SourceSql varchar(8000),@StrSeprate varchar(10))returns @temp table(F1 varchar(100))asbegin declare @i int set @SourceSql = rtrim(ltrim(@SourceSql)) set @i = charindex(@StrSeprate,@SourceSql) while @i >= 1 begin if len(left(@SourceSql,@i-1))>0 begin insert @temp values(left(@SourceSql,@i-1)) end set @SourceSql=substring(@SourceSql,@i+1,len(@SourceSql)-@i) set @i=charindex(@StrSeprate,@SourceSql) end if @SourceSql <> '' insert @temp values(@SourceSql) returnendselect * from split(',,777,,11,,888,88,,1122,888,88,77,,00,,00',',')