当前位置: 代码迷 >> Sql Server >> sql 截取各个字符串解决方案
  详细解决方案

sql 截取各个字符串解决方案

热度:100   发布时间:2016-04-27 10:51:58.0
sql 截取各个字符串
“汇总:角码\中梃连接件\垫条\拉杆\拼管压板下料574个,中梃连接件钻孔攻丝及装箱156个,加工泡沫塞块及装箱248块。”
这样一个字符串,想分别截出
角码\中梃连接件\垫条\拉杆\拼管压板下料574个
中梃连接件钻孔攻丝及装箱156个
加工泡沫塞块及装箱248块


求方法

------解决方案--------------------
那加一个判断咯
SQL code
declare @sc nvarchar(200),@begin int,@sql nvarchar(900)set @sc =',汇总:下一般型材料263根,'--set @sc =',1,1,1,1,1,'set @begin=1if CHARINDEX(',',@sc,@begin)=0    set @sql='select '''+ @sc+''''else    begin        while CHARINDEX(',',@sc,@begin)>0            begin                 if isnull(@sql,'')=''                    set @sql='select '''+ substring(@sc,@begin,CHARINDEX(',',@sc,@begin)-1)+''''                else                    set @[email protected]+' union all select '''+ substring(@sc,@begin,CHARINDEX(',',@sc,@begin)[email protected])+''''                set @begin=CHARINDEX(',',@sc,@begin)+1                print @begin            end        --最后一个是逗号不取         if @begin -1<LEN(@sc)            set @[email protected]+' union all select '''+ substring(@sc,@begin,LEN(@sc)-(@begin-1))+''''    end exec (@sql)
  相关解决方案