|
-
Aug 21st, 2004, 04:49 AM
#1
Sql Coalesce
What is "COALESCE"? This is most likely in T-SQL.
-
Aug 21st, 2004, 07:32 AM
#2
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 : 
-
Aug 21st, 2004, 07:56 AM
#3
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|