CONCAT is not working when column is NULL

3

I need help with the following code:

UPDATE propostas SET obs = CONCAT(obs, 'aaa'), cd_status = 0 WHERE tsk_id = 1

If the obs column is null, it does not load the new note.

Could anyone help me?

    
asked by anonymous 22.10.2018 / 20:10

1 answer

4

Use COALESCE :

UPDATE propostas SET obs = CONCAT( COALESCE(obs,""), 'aaa'), cd_status = 0 WHERE tsk_id = 1

The COALESCE gets a list of values, and returns the first non-null of it. In this case it will return obs if it is not null, and the string "" if obs is null.


You could do this instead of COALESCE , but it gets less elegant:

IF( obs IS NOT NULL, obs, "" )

IF tests the first parameter to true or false. If true, returns the second parameter, if false, returns the third.

In languages where there is no IF (nor IIF , as in T-SQL before 2012) has CASE IF , but it's a bit of a question.


Understand better on:

  

What's the difference between ISNULL and COALESCE in a search?

    
22.10.2018 / 20:11