Re: How to set a value when NULL - Mailing list pgsql-general

From David G. Johnston
Subject Re: How to set a value when NULL
Date
Msg-id CAKFQuwZHUWD7ZXWT9d-4UfMWLBN50pAPofq-4tFkN2JqKJ0_pw@mail.gmail.com
Whole thread Raw
In response to Re: How to set a value when NULL  ("sivapostgres@yahoo.com" <sivapostgres@yahoo.com>)
List pgsql-general
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.

pgsql-general by date:

Previous
From: Kevin Brannen
Date:
Subject: RE: Patterns to look for in the PostgreSQL server log
Next
From: Michael Lewis
Date:
Subject: Re: Force WAL cleanup on running instance