LXXIII LXXIII - 11 months ago 24
MySQL Question

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.


You can use coalesce:

SELECT COALESCE(email, 'Unknown')
FROM   customers