Sfwd favicon 152

How To Concat With Null Fields In MySQL

Posted by Weston Ganger

February 16, 2017

I needed to CONCAT a string in MySQL statement but it had null values which where wrecking the statement. Turns out you need to use CONACT_WS instead. This allows NULL values to be treated as empty strings as you would expect

# Fails
CONCAT('', 'foo', 'bar', NULL) = NULL

# Allows any null values
CONCAT_WS('', 'foo', 'bar', NULL) = 'foobar'

Related External Links:

Posted in Web Development and Tagged with mysqlsql

Recommended Posts