ledesma
ledesma

Reputation: 378

Update field as null from a select that returns 0 rows

I am trying to update a field with a null value when the select doesn't return values.

Eg:

UPDATE table SET title = COALESCE(a.title, NULL) 
FROM (
SELECT '1' AS title WHERE 0 = 1) a
WHERE id = 1

So, the select that the update is updating from is not returning any rows, the title should get null value.

Upvotes: 0

Views: 51

Answers (1)

krokodilko
krokodilko

Reputation: 36107

Use:

update  t set title = (SELECT '1' AS title WHERE 0 = 1)
where id = 1

Upvotes: 1

Related Questions