Whenever you deal with NULL touching an expression and want to avoid rendering the result as NULL, a solution is required to break out of the cycle. With the SQL COALESCE function, you can test for NULL, and when present, substitute NULL for another value. This post takes a look at COALESCE and also compares SQL COALESCE to CASE.
Knowing when to use the SQL
COALESCE function is a lifesaver when you’re dealing with NULL.
As you know, NULL is a tricky concept, and it seems whatever NULL “touches” in an expression, it renders the result NULL. So, when you’re dealing with NULL, how can you break out of the cycle? That is, how can you display another value instead?
This is where SQL
COALESCE comes into play. With this function, you can test for NULL, and when present, substitute NULL for another value.
What is COALESCE?
COALESCE is a built-in SQLServer Function. Use
COALESCE when you need to replace a NULL with another value. It takes the form:
COALESCE(value1, value2, ..., valuen)
It returns the first non NULL from the value list.
Consider the baseball parks from Lahmans Baseball Database. If we want to display the parkname in place of the parkalias, which may be null,
COALESCE works well here:
SELECT COALESCE(parkalias, parkname) as ParkAlias, city, state FROM Parks
SELECT COALESCE(parkalias, parkname) as ParkAlias,
In this example,
COALESCE(parkalias, parkname) returns the value found in
parkalias if the value is not NULL; however, if it is NULL, then
parkname is returned.
What SQL would you write if you wanted to display ‘
Unknown’ if the
NULL? Why not try to write the query… here is a start…
SELECT parkalias, parkname, city, state FROM Parks
SELECT COALESCE(parkalias, 'Unknown') as parkalias,
Comparing SQL COALESCE to CASE
Did you know SQL
COALESCE is a shortcut for the CASE statement?
Consider the table survey, which has the columns
We want to list the
answerID and first non NULL option.
From what we have learned, we can use
COALESCE(option1, option2, option3, 'No Option Selected')
This is a shortcut for this
WHEN option1 is not NULL then option1
WHEN option2 is not NULL then option2
WHEN option3 is not NULL then option3
ELSE 'No Option Selected'
CASE is used in more circumstances, but when replacing
COALESCE is a handy shortcut!
For the record, SQL Server has many more interesting functions to learn and explore. If you haven’t done so already, check out these string functions to manipulate text data!
Hello my name is Kris. I’m here because I am passionate about helping non-techie people to overcome their fear of learning SQL.
I know what it is like to not know where to start or whether the time spent learning is worth the effort. That is why I am here to help you to:
- Get started in an easy to follow step-by-step manner.
- Use your time wisely so you focus on what is important to learn to get the most value from your time.
- Answer your questions. Really! Just post a comment and I’ll respond. I’m here to help.
It wasn’t long ago that I was helping a colleague with some reporting. She didn’t know where to start and soon got overwhelmed and lost as she didn’t know SQL.
I felt really bad, as she was under pressure to get some summary information to her boss, the built-in reports were falling short, and to make them better would require her to know SQL. At that time that seemed impossible! It in dawned on me, it doesn’t have to be that way.
Then I discovered a way for anyone with the desire to easily learn SQL. I worked with my co-worker, started to teach her what I learned and soon she was able to write reports and answer her boss’ questions without getting stressed or ploughing hours into manipulating data in Excel.
It hasn’t always been easy. Sometimes the information seems abstract or too conceptual. In this case I’ve found out that a visual explanation is best. I really like to use diagrams or videos to explain hard-to-grasp ideas.
Having video, pictures, and text really help to reinforce the point and enable learning.
And now I want to help you get the same results.
The first step is simple, click here http://www.essentialsql.com/get-started-with-sql-server/