MySQL: NULL to 0 or any other value


Converting null to 0 or any other value in mysql

COALESCE(colname,0)

Returns the first non-NULL value in the list, or NULL if there are no non-NULL values.

usage :

SELECT COALESCE(colname, 0);
SELECT COALESCE(NULL,1);

how it works:

Parameter can be a table column or a static value, if table column name is given it searches for column value in database, if column value is not null it will return column value other wise it wil look for next value in paramteres and continues until it finds out a not null value

example:

SELECT COALESCE(column, 0);
if column name is null it will return 0, as 0 is next not null value in parameters

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s