Posted about 7 years ago. Visible to the public. Repeats.

MySQL: CONCAT with NULL fields

In MySQL,

Copy
CONCAT('foo', 'bar', NULL) = NULL

the NULL always wins.

If you would rather treat NULL as an empty string, use CONCAT_WS (concatenation with separator) instead:

Copy
CONCAT_WS('', 'foo', 'bar', NULL) = 'foobar'

By refactoring problematic code and creating automated tests, makandra can vastly improve the maintainability of your Rails application.

Author of this card:

Avatar
Tobias Kraze
Last edit:
about 3 years ago
by Arne Hartherz
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Tobias Kraze to makandropedia