This question already has an answer here:
- nvarchar concatenation / index / nvarchar(max) inexplicable behavior 2 answers
I'm having some very strange behavior with coalesce. When I don't specify a return amount (TOP (50)) I'm only getting a single last result, but if I remove the "Order By" it works... Examples below
DECLARE @result varchar(MAX)
SELECT @result = COALESCE(@result + ',', '') + [Title]
FROM Episodes WHERE [SeriesID] = '1480684' AND [Season] = '1' Order by [Episode] ASC
SELECT @result
Will only return a single last result:
The Shiva Bowl
But If I specifiy a max return amount (only adding TOP (50) to same statement)
DECLARE @result varchar(MAX)
SELECT TOP(50) @result = COALESCE(@result + ',', '') + [Title]
FROM Episodes WHERE [SeriesID] = '1480684' AND [Season] = '1' Order by [Episode] ASC
SELECT @result
I get all the results in proper order
The Draft,The Bounce Test,Sunday at Ruxin's,Mr. McGibblets,The Usual Bet,The Shiva Bowl
The culprit seems to be the [Title] column as if I return a different column it seems to be working without specifying a return limit. FYI [Title] is a VARCHAR(MAX) NOT NULL column.
Any insight what could be causing this? I really don't want to set a limit, but it's the only way it's returning all the data at the moment... Thanks