sqlserver2008 拆分字符串

前端之家收集整理的这篇文章主要介绍了sqlserver2008 拆分字符串前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

--说明:例如,将下列数据 id id_value
-----------------
1 'aa,bb'
2 'aa,bb,cc'
3 'aaa,bbb,ccc' --转换成以下的格式
id id_value
-----------------
1 'aa'
1 'bb'
2 'aa'
2 'bb'
2 'cc'
3 'aaa'
3 'bbb'
3 'ccc' --代码-------------------------------------------GO
<div class="codetitle"><a style="CURSOR: pointer" data="44352" class="copybut" id="copybut44352" onclick="doCopy('code44352')"> 代码如下:

<div class="codebody" id="code44352">
create table ta
(
id int,
id_value varchar(30)
) insert into ta
select 1,'aa,bb' union all
select 2,cc' union all
select 3,'aaa,ccc' select
top 4000 ix = identity(int,1,1)
into #tb
from sys.objects a,sys.objects b select
id,
id_value = substring(id_value,ix,charindex(',',id_value + ',ix) - ix)
FROM ta as A,#tb as B
WHERE SUBSTRING( ',' + id_value,1) = ',' drop table ta
drop table #tb

猜你在找的MsSQL相关文章