具有多个字段的XML路径的SQL Stuff

时间:2017-05-04 18:32:48

标签: sql sql-server tsql concatenation

我正在使用SQL Server 2008.我正在尝试使用STUFF和FOR XML PATH编写一个select语句,以在一个字段中创建逗号分隔的字符串。只有一个记录,但有31个字段。

我从1个表中提取数据,该表每月的每一天都有一个smallint字段 - 0表示当天没有,1表示是。 (我没有设计这个表,不幸的是我不允许改变它。)像这样:

z1st   z2nd   z3rd   z4th   z5th   z6th   z7th   z8th   z9th   z10th
------ ------ ------ ------ ------ ------ ------ ------ ------ ------
0      1      0      0      0      0      0      0      0      0
0      0      0      0      1      0      0      0      0      1
0      0      1      0      0      0      0      0      0      0
0      0      0      1      0      0      0      0      0      0
0      1      0      0      0      0      0      0      0      1

等等,一直到z31st。

我的声明是传递整数变量@order_snbr和@admin_nbr,它总会返回一行。

DECLARE @strSelectedDayList AS VARCHAR(MAX)

SELECT @strSelectedDayList = STUFF((SELECT ', ' + CASE WHEN td1.z1st = 1 THEN '1st' END
, CASE WHEN td1.z2nd = 1 THEN '2nd' END
, CASE WHEN td1.z3rd = 1 THEN '3rd' END
, CASE WHEN td1.z4th = 1 THEN '4th' END
, CASE WHEN td1.z5th = 1 THEN '5th' END
, CASE WHEN td1.z6th = 1 THEN '6th' END
, CASE WHEN td1.z7th = 1 THEN '7th' END
, CASE WHEN td1.z8th = 1 THEN '8th' END
, CASE WHEN td1.z9th = 1 THEN '9th' END
, CASE WHEN td1.z10th = 1 THEN '10th' END
, CASE WHEN td1.z11th = 1 THEN '11th' END
, CASE WHEN td1.z12th = 1 THEN '12th' END
, CASE WHEN td1.z13th = 1 THEN '13th' END
, CASE WHEN td1.z14th = 1 THEN '14th' END
, CASE WHEN td1.z15th = 1 THEN '15th' END
, CASE WHEN td1.z16th = 1 THEN '16th' END
, CASE WHEN td1.z17th = 1 THEN '17th' END
, CASE WHEN td1.z18th = 1 THEN '18th' END
, CASE WHEN td1.z19th = 1 THEN '19th' END
, CASE WHEN td1.z20th = 1 THEN '20th' END
, CASE WHEN td1.z21st = 1 THEN '21st' END
, CASE WHEN td1.z22nd = 1 THEN '22nd' END
, CASE WHEN td1.z23rd = 1 THEN '23rd' END
, CASE WHEN td1.z24th = 1 THEN '24th' END
, CASE WHEN td1.z25th = 1 THEN '25th' END
, CASE WHEN td1.z26th = 1 THEN '26th' END
, CASE WHEN td1.z27th = 1 THEN '27th' END
, CASE WHEN td1.z28th = 1 THEN '28th' END
, CASE WHEN td1.z29th = 1 THEN '29th' END
, CASE WHEN td1.z30th = 1 THEN '30th' END
, CASE WHEN td1.z31st = 1 THEN '31st' END
FROM TableDays td1 WITH(NOLOCK) 
WHERE td1.order_snbr = td2.order_snbr 
AND td1.admin_nbr = td2.admin_nbr 
FOR XML PATH('')), 1, 1, '')
FROM TableDays td2 WITH(NOLOCK)
WHERE td2.order_snbr = @order_snbr
AND td2.admin_nbr = @admin_nbr

PRINT @strSelectedDayList

这会产生如下结果:

1st14th28th

我想要的是:

1st, 14th, 28th

任何帮助将不胜感激!如果有更好的方法(没有STUFF或FOR XML PATH),我很乐意转换。

1 个答案:

答案 0 :(得分:1)

也许是另一种方法

示例

Declare @YourTable Table ([z1st] varchar(50),[z2nd] varchar(50),[z3rd] varchar(50),[z4th] varchar(50),[z5th] varchar(50),[z6th] varchar(50),[z7th] varchar(50),[z8th] varchar(50),[z9th] varchar(50))
Insert Into @YourTable Values
 (0,1,0,0,0,1,0,0,0)
,(0,0,0,0,1,0,0,1,0)
,(0,0,1,0,0,0,0,0,0)
,(1,1,1,1,0,0,0,0,0)
,(0,1,0,0,0,0,1,0,1)

Select A.*
      ,C.* 
 From  @YourTable A
 Cross Apply (Select XMLData = cast((select A.* for XML Raw) as xml) ) B
 Cross Apply (
                Select S = Stuff((Select ', ' +replace(Item,'z','')
                 From (
                        Select Item   = attr.value('local-name(.)','varchar(100)')
                         From  B.XMLData.nodes('/row') as A(r)
                         Cross Apply A.r.nodes('./@*') AS B(attr)
                         Where attr.value('local-name(.)','varchar(100)') like 'z[0-9]%'
                           and attr.value('.','varchar(max)') =1
                      ) C1
                 For XML Path ('')),1,2,'') 
             ) C

<强>返回

enter image description here