That function doesn't use COALESCE to concatenate a string. It concatenates using a variable assignment in the SELECT. Unfortunately the result of assignment in a multi-row SELECT is officially undefined. You could try adding ORDER BY to that statement but there are no guarantees and even if it works today it could break in future under some service pack or hotfix or even for no apparent reason at all. I definitely don't recommend using this kind of kludge.
Instead try:
SELECT DISTINCT OrderId,
STUFF(
(SELECT ' '+CommentText AS [text()]
FROM Orders
WHERE OrderId = T.OrderId
ORDER BY seq
FOR XML PATH( '' )
), 1,1,'') AS concat
FROM Orders AS T;
Main Topics
Browse All Topics





by: BriCrowePosted on 2008-03-14 at 16:06:01ID: 21130403
Just add an ORDER BY clause
SELECT @res = COALESCE(@res + ', ' , '') + fibretype + ' ' + cast(FibrePercent as varchar(10)) + '%' as PercentDisplay
FROM yourtable
WHERE id = @id
ORDER BY Seq