SQL COALESCE() Like ISNULL() On Steroids ... With Caveats

Posted August 4, 2006 at 5:04 PM

Tags: SQL

My main man, Rick O, just told me about the SQL statement COALESCE(). He had pointed out that my use of ISNULL() is not as universally accepted on DB platforms as COALESCE(). This function, as he explained, takes a list of items and returns the first one that is not NULL. After looking it up, I also see that if no values are non-null, then the function call itself returns NULL.

The main advantage here is that you can have multiple fall back values. As an example, let's say you wanted to return someone's phone number. If they had a business phone, mobile phone, and page, you could return which ever one was available (in order of importance):

 Launch code in new window » Download code as text file »

  • SELECT
  • COALESCE(
  • business_phone,
  • cell_phone,
  • pager
  • ) AS phone_number
  • FROM
  • contact

I think this is a very cool function. The one caveat is that you cannot mix data types in your list. This of course is not something that will come up very open, but, it's worth knowing about. Now, this doesn't always throw an error. Sometimes it works, sometimes it does not; it depends on where null values are found and which data types are being used.

For example, this does NOT throw an error:

 Launch code in new window » Download code as text file »

  • SELECT
  • COALESCE(
  • -- DB date/time field.
  • began_dating,
  •  
  • -- System date/time.
  • getDate(),
  •  
  • -- Text expression
  • 'sometime'
  • ) AS began_dating
  • FROM
  • girl

Even though 'sometime' is not of type date/time, no errors are thrown. In fact, you can force a null to be found and still no errors are thrown:

 Launch code in new window » Download code as text file »

  • SELECT
  • COALESCE(
  • -- Force null value.
  • NULL,
  •  
  • -- System date/time.
  • getDate(),
  •  
  • -- Text expression
  • 'sometime'
  • ) AS began_dating
  • FROM
  • girl

Works fine. However, if you switch the second and third arguments, such that the string value comes before the date value, it DOES throw an error:

 Launch code in new window » Download code as text file »

  • SELECT
  • COALESCE(
  • -- Force null value.
  • NULL,
  •  
  • -- Text expression
  • 'sometime',
  •  
  • -- System date/time.
  • getDate()
  • ) AS began_dating
  • FROM
  • girl

This throws the error:

Syntax error converting date/time from character string.

So again, this is rarely gonna be an issue, if ever, but still, good to know that there are errors sometimes and why they might seem random.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page




Reader Comments

Oct 2, 2007 at 4:14 AM // reply »
1 Comments

Clearly the advantage of multiple fallbacks is not exclusive to coalesce.

Merely do ISNULL(A, ISNULL(B, ISNULL(C, D))) to choose between 4 values.


Daniel Tilly
Aug 18, 2008 at 8:09 AM // reply »
1 Comments

Yeah but those close brackets can get way out of control ...


Post Comment  |  Ask Ben

Recent Blog Comments
Secret Admirer
Jul 4, 2009 at 12:23 PM
Project HUGE: Huge In A Hurry - Get Big - Phase 2 / Week 3
My Poor Dreamboat :( I feel so sad when I know you are hurting. I hope you feel better soon. ... read »
Jul 4, 2009 at 9:42 AM
FLV 404 Error On Windows 2003 Server
I bookmarked this page. Thanks for given this great post.... ... read »
Jul 4, 2009 at 4:00 AM
Terms Of Service / Privacy Policy Document Generator
thanks ben, I'm not a big fan of contracts so to find your no no-nesense ToS generator has helped me no end. all the best matt ... read »
Justice
Jul 3, 2009 at 11:10 PM
Create A Running Average Without Storing Individual Values
@Ben, I think you're going about this the wrong way. You're trying to use complicated techniques when there is a simple and beautiful technique readily available (a la Gary Funk's comment). Instead ... read »
Bob
Jul 3, 2009 at 9:19 PM
Project HUGE: Huge In A Hurry - Get Big - Phase 3 / Week 1
a good technical explanation http://crossfitphoenix.typepad.com/crossfit_phoenix_forging_/the-overhead-squat.html ... read »
Jul 3, 2009 at 9:03 PM
Create A Running Average Without Storing Individual Values
If I wanted to do this and only carry two numbers, I'd keep track of the sum and N. Then you are pretty much accurate all the time. average = (sum + new_number) / (N + 1) But all this was in a for ... read »
Roland Collins
Jul 3, 2009 at 8:58 PM
Create A Running Average Without Storing Individual Values
@Martin - not just floating point though. Depending on what langauge you're working in, decimals can cause just as many headaches if they're not precise enough. But again, for most applications, th ... read »
Isnogood
Jul 3, 2009 at 7:16 PM
Project HUGE: Huge In A Hurry - Get Big - Phase 3 / Week 1
Watch this http://www.nsca-lift.org/videos/default.shtml ... read »