Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL query that returns specific string if column is null?

When an SQL query returns null, I'm wondering if it's possible to have it return a specific string like "Unknown" instead of null.

I'm currently doing this on the JavaScript side and was wondering if there's a faster way to do this on the server.

Example (I realize the following syntax doesn't exist):

SELECT Customers.Email (RETURN "Unknown" IF NULL)

I imagine it's possible with a CASE? But filling up my query with CASE statements will slow this whole action down rather than speed it up.

like image 455
Sam Malayek Avatar asked Jun 06 '16 07:06

Sam Malayek


1 Answers

You can use coalesce:

SELECT COALESCE(email, 'Unknown')
FROM   customers
like image 78
Mureinik Avatar answered Oct 20 '22 18:10

Mureinik