Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

FOR XML and the need for DISTINCT

1 view
Skip to first unread message

Dan Holmes

unread,
Aug 26, 2010, 8:22:11 AM8/26/10
to
The following code will only include the 'OFFERED' value once but it will leave a trailing comma. If the DISTINCT is
added the trailing comma is gone. Why? If the results are returned instead of placing them in the variable then both
'OFFERED' are there.

Change tblCalendar to your own numbers table.

DECLARE @coordination_state_cds VARCHAR(20), @Filters INT;
SET @Filters = 16424
SET @coordination_state_cds = REPLACE(
(
SELECT CASE
--SELECT DISTINCT CASE
WHEN @Filters & POWER(2,id) = 8 THEN 'OFFERED'
WHEN @Filters & POWER(2,id) = 32 THEN 'COORDINATED'
WHEN @Filters & POWER(2,id) = 16384 THEN 'OFFERED'
END as 'data()'
FROM tblcalendar
--3 is 8 and 14 16384, only need that range.
WHERE ID BETWEEN 3 AND 14
FOR XML PATH('')
), ' ', ',');
PRINT @coordination_state_cds

thanks
dan

Mark

unread,
Aug 26, 2010, 11:56:01 AM8/26/10
to

Change

DECLARE @coordination_state_cds VARCHAR(20)

to

DECLARE @coordination_state_cds VARCHAR(100)

0 new messages