22,209
社区成员
发帖
与我相关
我的任务
分享
use Tempdb
go
--> --> 中国风(Roy)生成測試數據
if not object_id(N'Tempdb..#tabl') is null
drop table #tabl
Go
Create table #tabl([COl] nvarchar(24))
Insert #tabl
select N'白铁桶' union all
select N'保险锁' union all
select N'插心门锁' union all
select N'窗帘夹' union all
select N'肥皂合' union all
select N'硷性电池'
Go
SELECT STUFF((SELECT ','+QUOTENAME([COl],'''') from #tabl FOR XML PATH('')),1,1,'')
/*
'白铁桶','保险锁','插心门锁','窗帘夹','肥皂合','硷性电池'
*/
--3.合并字符串
create table test4 (name varchar(10),mytype varchar(10),cj int )
insert into test4
values('张三','语文',83),
('张三','数学',65),
('张三','物理',85),
('李四','语文',73),
('李四','数学',69),
('李四','物理',93)
select name,
stuff((select ','+mytype from test4 t4 where t4.name = test4.name for xml path('')),1,1,'') as mytype,
stuff((select ','+cast(cj as varchar) from test4 t4 where t4.name = test4.name for xml path('')),1,1,'') as cj
from test4
group by name
/*
name mytype cj
李四 语文,数学,物理 73,69,93
张三 语文,数学,物理 83,65,85
*/
在论坛中出现的比较难的sql问题:27(字符串拆分、字符串合并、非连续数字的间隔范围、随机返回字符串)
http://blog.csdn.net/sqlserverdiscovery/article/details/39250837