MySQL Concat NULL columns

When you want to combine values of multiple columns, CONCAT function comes to help. It’s very straightforward until one of the columns can contain NULL values.
For example if column column1 is NULL and column2 is ‘Hello’, following query

SELECT CONCAT(column1, column2) FROM test WHERE id = 1

will return NULL. Sad face.

There are multiple solutions to this, but most easy one is to use COALESCE function. It will return first non-NULL value of a list. In this case, it will return empty string if column1 is NULL.
So query

SELECT CONCAT(COALESCE(column1,''), column2) FROM `test` WHERE id = 1

will return ‘Hello’. Success!
If both columns can contain NULL values, then use

SELECT CONCAT(COALESCE(column1,''), COALESCE(column2,'')) FROM `test` WHERE id = 1

and so on.


Deprecated: Non-static method dc_jqslicksocial::footer() should not be called statically in /var/www/digitalette.com/wp-includes/class-wp-hook.php on line 298

Notice: Undefined index: show_post in /var/www/digitalette.com/wp-content/plugins/slick-social-share-buttons/inc/dcwp_social.php on line 805