The original test data was....
[tt]
INSERT INTO t VALUES (1,'a');
INSERT INTO t VALUES (1,'b');
INSERT INTO t VALUES (1,'c');
INSERT INTO t VALUES (1,'d');
INSERT INTO t VALUES (2,'e');
INSERT INTO t VALUES (2,'f');
INSERT INTO t VALUES (3,'g');
INSERT INTO t VALUES (4,'h');
[/tt]
You get this:
[tt][blue]
id Cols
----------- -----------------
1 a,b,c,d
2 e,f
3 g
4 h
[/blue][/tt]
If you add...
[tt]
INSERT INTO t VALUES (1,NULL);
INSERT INTO t VALUES (1,'X');
INSERT INTO t VALUES (1,'Y');
[/tt]
You now get...
[tt][blue]
id Cols
----------- -----------------
1 X,Y
2 e,f
3 g
4 h
[/blue][/tt]
From id = 1, the a,b,c,d is missing. The problem is with the function and is easily fixed.
Code:
ALTER FUNCTION [dbo].[ConcatenateCols](@Id INT)
RETURNS VARCHAR(MAX)
AS
BEGIN
DECLARE @RtnStr VARCHAR(MAX)
SELECT @RtnStr = COALESCE(@RtnStr + ',','') + col
FROM dbo.t
WHERE id = @Id
[!]And col > ''[/!]
RETURN @RtnStr
END
By adding "And col > ''" you are filtering out the nulls and the empty strings. This problem is pretty well explained here: thread183-1159740
-
George
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom