PaulJames
PaulJames

Reputation: 95

MySQL Can't seem to concat null field

MySQL will not update a null or empty field, but seems ok if I add something to the field before hand.

UPDATE trip set MYZID=concat(MYZID, '1,') WHERE id=93

Upvotes: 2

Views: 120

Answers (3)

forpas
forpas

Reputation: 164089

The function concat() as stated in the documentation:

returns NULL if any argument is NULL

so to make it work use coalesce():

UPDATE trip set MYZID=concat(coalesce(MYZID, ''), '1,') WHERE id=93

Upvotes: 3

Nir Levy
Nir Levy

Reputation: 12953

You should use ifnull function and set it to empty string in case of null

UPDATE trip set MYZID=concat(ifnull(MYZID, ''), '1,') WHERE id=93

Upvotes: 1

JustAnotherCoder
JustAnotherCoder

Reputation: 393

From https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_concat

CONCAT() returns NULL if any argument is NULL.

Upvotes: 2

Related Questions