Re: How to set a value when NULL

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: "sivapostgres(at)yahoo(dot)com" <sivapostgres(at)yahoo(dot)com>
Cc: Postgresql General Group <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: How to set a value when NULL
Date: 2020-03-11 13:45:52
Message-ID: CAKFQuwZHUWD7ZXWT9d-4UfMWLBN50pAPofq-4tFkN2JqKJ0_pw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wednesday, March 11, 2020, sivapostgres(at)yahoo(dot)com <sivapostgres(at)yahoo(dot)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.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Michael Lewis 2020-03-11 14:42:42 Re: Force WAL cleanup on running instance
Previous Message Kevin Brannen 2020-03-11 13:14:51 RE: Patterns to look for in the PostgreSQL server log