I've used an adaptation of shnugo's answer to Select all XML nodes from XML column , and this works a treat. However, the generated list has a space inserted between items.
Is there a way to prevent the space separator from being included in the output, or to specify the separator to be a comma?
I've seen a reference to it somewhere in my travels but can't locate the link any more.
The following (updated following initial feedback):
declare @table TABLE(ID int, IsColA bit, IsColB bit, IsColC bit, IsColD bit, IsColE bit)
insert @table select 1, 1, 1, 1, 1, 0
SELECT * FROM (
SELECT TagName +',' AS [data()] FROM (
SELECT ID AS ID
, Nodes.value('local-name(.)','varchar(32)') AS TagName
, Nodes.value('text()[1]','bit') AS TagValue
FROM (
SELECT ID, CONVERT(XML, (SELECT pbInner.* FOR XML PATH('row'))) as Rows
FROM @table pbInner
WHERE pbInner.ID = 1
) t
CROSS APPLY t.Rows.nodes('/row/*') A(Nodes)
) cols
WHERE cols.TagName LIKE 'is%' and cols.TagName NOT IN ('IsToBeExcluded', 'IsAlsoToBeExcluded')
AND TagValue = 1
FOR XML PATH('')
) inds(indlist)
Gives output IsColA, IsColB, IsColC, IsColD,
when ideally it would output IsColA,IsColB,IsColC,IsColD,
I realise that I can do Replace(indlist, ' ', '')
but would like to know if there's a way to do it within the XQuery.
I'm happy to hear of a better way of doing the above, but note that the database is at compatibility mode 100 (Sql Server 2008) which I believe precludes the use of FOR JSON.