Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL, coalesce equivalent for empty values?

Tags:

sql

mysql

I know that coalesce will return the first non-null value that is passed to it. is there something simlilar that will return the first not empty / not false value?

example:

select FunctionIWant(0,'','banana') as fruit;  //returns banana. 
like image 574
GSto Avatar asked Nov 04 '10 21:11

GSto


People also ask

Does coalesce work with empty strings?

The Coalesce function evaluates its arguments in order and returns the first value that isn't blank or an empty string. Use this function to replace a blank value or empty string with a different value but leave non-blank and non-empty string values unchanged.

What does coalesce return if all values are NULL?

If all the values in MySQL COALESCE() function are NULL then it returns NULL as the output. It means that this function does not find any non-NULL value in the list.

Is NULL and coalesce MySQL?

The COALESCE() function accepts one parameter which is the list which can contain various values. The value returned by the MySQL COALESCE() function is the first non-null value in a list of expressions or NULL if all the values in a list are NULL.

Is there coalesce in MySQL?

The COALESCE() function in MySQL is used to return the first non-null value in a specified series of expressions. If this function evaluates all values of the list are null, or it does not find any non-null value, then it returns NULL.


1 Answers

You could make NULL from empty string in MySQL :

SELECT coalesce(NULLIF(email, ''), '[email protected]') FROM users WHERE id=1000000; 
like image 196
Fedir RYKHTIK Avatar answered Oct 07 '22 09:10

Fedir RYKHTIK