Navigation | SQL Coalesce

SQL Coalesce

Have you guys ever seen the Coalesce command in SQL? I'd never heard of it til today, and it looks really useful.

Say you have a table of business contact information and you want to contact a user from it; but some of the businesses don't have specific contacts, just a position; and some don't have either…

SELECT COALESCE(ContactName,ContactPosition,BusinessName) as theContact FROM foo

If ContactName is null, theContact will be the value of ContactPosition. If that's also null, it will be the value of BusinessName. Simple. handy. wish I'd known it 5 years ago.

Filed by JC at November 4th, 2010 under ColdFusion, SQL

Leave a comment