June 13, 2009 at 10:20 am
Comments posted to this topic are about the item Using Coalesce in a stored Procedure
June 26, 2009 at 12:32 am
Please correct me if I've misunderstood but....
Coalesce returns the first non-null value among its parameters (from first to last). Given that your second parameter is
not null (a space), the third parameter is superfluous.
COALESCE(@COUNTYCRITERIA,' ',',')
If you initialise your variable @COUNTYCRITERIA to an empty string first, then you don't have to coalesce anything.
i.e.
DECLARE @COUNTYCRITERIA varchar(1000)
SET @COUNTYCRITERIA=''
SELECT....
Regards,
David McKinney.
June 26, 2009 at 6:00 am
I find the use of XMl to be a cleaner method to concatenate rows into a single value:
select Fname+','
From Employee
For XML Path(''), Type
June 26, 2009 at 6:59 am
I'm with David, the use of two rightmost strings in the COALESCE function doesn't make sense. You'll never get to the last (third) parameter. With COALESCE only the last (rightmost) parameter should ever be a fixed value and each of those to the left of it should be variables or columns that could be null.
June 26, 2009 at 7:13 am
All great comments!! I agree that XMI might give you cleaner results. this example was written to show how to concatinate rows when XMI isnt an option in stored procedures or SQL only is required.
June 26, 2009 at 7:52 am
Agreed, but XML constructs are part of T-SQL and therefore, always available
June 26, 2009 at 1:35 pm
Edward Boyle (6/26/2009)
Agreed, but XML constructs are part of T-SQL and therefore, always available
If you are using SQL Server 2005 and above... FOR XML PATH is not available in SQL Server 2000.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply