Interestingly enough I have found the issue. It appears to be 2 fold...
It appears that the
.value('text()[1]','nvarchar(max)'),1,2,'')
part of theSTUFF
query was the part that was not displaying data.When I change the
text()
to a.
i.e..value('.[1]','nvarchar(max)'),1,2,'')
the query returns as expected.Secondly I had forgotten to add the
N', ' +
to theISNULL(scddisc.abbrev, '!!') AS medicalDisciplines
to make it a comma separated list as required.
Therefore the whole new statement became:
SELECT
STUFF (
(
SELECT
*
FROM
(
SELECT
N', ' + ISNULL(scddisc.abbrev, '!!') AS medicalDisciplines
FROM
personenc
INNER JOIN contacts ON personenc.cntid = contacts.cntid
INNER JOIN scddisc ON contacts.discipline = scddisc.disid
WHERE
(personenc.perid = 15410)
AND (scddisc.active = 1)
AND (contacts.active = 1)
AND (scddisc.medgroup = 1) --
AND (
personenc.sdate BETWEEN '2014-03-05'
AND '2014-03-12'
)
UNION
SELECT
N', ' + ISNULL(scddisc.abbrev, '!!') AS medicalDisciplines
FROM
scddisc
INNER JOIN contacts ON scddisc.disid = contacts.discipline
INNER JOIN personenc ON contacts.cntid = personenc.ocntid
WHERE
(personenc.perid = 15410)
AND (scddisc.active = 1)
AND (contacts.active = 1)
AND (scddisc.medgroup = 1) -- AND (personenc.sdate BETWEEN '2014-03-05' AND '2014-03-12')
) AS k FOR XML PATH (''),
TYPE ).
VALUE
('.[1]', 'nvarchar(max)'),
1,
2,
''
);
Can anyone tell me what the meaning/difference of .value('.[1]','nvarchar(max)'),1,2,'')
and .value('text()[1]','nvarchar(max)'),1,2,'')
is??