码迷,mamicode.com
首页 > 数据库 > 详细

sql字符串分割

时间:2014-06-03 01:00:32      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:c   style   class   blog   code   a   

字符串:declare @str varchar(1000)=‘0208588A_0208588,0098256A_0208588,0224208A_0224208,‘

分割结果:

            bubuko.com,布布扣

示例sql:

declare @str varchar(1000)='0208588A_0208588,0098256A_0208588,0224208A_0224208,'
--declare @str varchar(1000)='0080203A_0080203,0080215A_0080203,'

declare @sSplitChar varchar(1)=','
declare @tblExternal_id table(external_id varchar(100))
 
declare  @tmp   as   varchar(100)   
set   @str=rtrim(ltrim(@str)) + @sSplitChar     
set   @str=replace(@str,@sSplitChar+@sSplitChar, @sSplitChar) -- make sure @str ends with a @sSplitChar 

while(@str<>'')   
begin   
	set   @tmp=left(@str,charindex(@sSplitChar,@str,1)-1)   
	insert   @tblExternal_id   values(@tmp)   
	set   @str=stuff(@str,1,charindex(@sSplitChar,@str,1),'')   
end   
  
 --select * from @tblExternal_id
 select external_id, row_number() over(order by external_id) as RownNum from @tblExternal_id                 

参考来源:

http://www.360doc.com/content/12/0213/14/19692_186289261.shtml

sql字符串分割,布布扣,bubuko.com

sql字符串分割

标签:c   style   class   blog   code   a   

原文地址:http://blog.csdn.net/keenweiwei/article/details/27689405

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!