Re: How to set a value when NULL

Started by sivapostgres@yahoo.comabout 6 years ago2 messagesgeneral
Jump to latest
#1sivapostgres@yahoo.com
sivapostgres@yahoo.com

Hello,Need to set a value of Zero when the field value is NULL in trigger function.
Tried with,NEW.fieldname = NULLIF(NEW.fieldname, 0)
in before insert/update trigger.
Looks like it's not working.  I'm doing anything wrong.
Happiness AlwaysBKR Sivaprakash

#2David G. Johnston
david.g.johnston@gmail.com
In reply to: sivapostgres@yahoo.com (#1)
Re: How to set a value when NULL

On Wednesday, March 11, 2020, sivapostgres@yahoo.com <sivapostgres@yahoo.com>
wrote:

Hello,
Need to set a value of Zero when the field value is NULL in trigger
function.

Tried with,
NEW.fieldname = NULLIF(NEW.fieldname, 0)

in before insert/update trigger.

Looks like it's not working. I'm doing anything wrong

NULLIF does the inverse of what you want - you expression returns null if
fieldna,e has a value of 0.

COALESCE is what you want.

Coalesce(fieldname, 0) — returns the first, non-null argument.

David J.