Using Coalesce in a stored Procedure

  • Comments posted to this topic are about the item Using Coalesce in a stored Procedure

  • 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.

  • 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

  • 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.

  • 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.

  • Agreed, but XML constructs are part of T-SQL and therefore, always available

  • 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