CONCAT is not working when column is NULL

Asked

Viewed 179 times

3

I need help with the following code:

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

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

Could someone help me?

1 answer

4


Use the COALESCE:

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

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


I could do this instead of COALESCE, but it’s less elegant:

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

The IF tests the first parameter for 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 the CASE IF, but it already escapes a little of the question.


Understand better at:

What is the difference between ISNULL and COALESCE in a survey?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.