LXXIII LXXIII - 1 year ago 69
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.

Answer Source

You can use coalesce:

SELECT COALESCE(email, 'Unknown')
FROM   customers
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download