Results 1 to 3 of 3

Thread: Sql Coalesce

  1. #1

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Sql Coalesce

    What is "COALESCE"? This is most likely in T-SQL.

  2. #2
    Big D Danial's Avatar
    Join Date
    Jul 2000
    Location
    ASP.Net Forum
    Posts
    2,877

    Re: Sql Coalesce

    Originally posted by mendhak
    What is "COALESCE"? This is most likely in T-SQL.
    Never heard of it, But here is the MSDN explanation of it

    http://msdn.microsoft.com/library/de...ca-co_9dph.asp
    [VBF RSS Feed]

    There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.

    If I have been helpful, Please Rate my Post. Thanks.

    This post was powered by :

  3. #3
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263
    Coalesce is an ansi-standard-sql function.

    It returns the first non-NULL value in it's arguments.

    MS T-SQL has an IsNull function that works like Coalesce with "two-arguments".

    IsNull({some expression},'EXP IS NULL') will return one of two things:

    1) Return {some expression} if it is not null
    2) Otherwise returns the string 'EXP IS NULL'

    Coalesce({some expression},'EXP IS NULL') will do exactly the same thing...

    But:

    Coalesce({some exp1},{some exp2},{some exp3},'ALL EXP ARE NULL')

    will return the first expression that is not null, exp1, exp2, exp3 or return the final argument 'ALL EXP ARE NULL'

    I've been dis'ed by purist SQL folks for using ISNULL - but I can't type COALESCE as easily as ISNULL, so I prefer ISNULL - even if it won't work on all SQL platforms...

    A perfect example of where we use ISNULL is...

    SELECT ...
    ,ISNULL(TE.TEACHERNAME+' ('+CL.TEAID+')',CL.TEAID)
    ... FROM CLASSTABLE CL
    LEFT JOIN TEACHERTABLE TE ON TE.TEAID=CL.TEAID

    Here we join a teacher table to a class table - if the teacher doesn't exist in the teacher table, then TE.TEACHERNAME is null - making the whole expression NULL - so we return just the CL.TEAID in that case.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width