From: John W Higgins <wishdev@gmail.com>
To: pgsql-general <pgsql-general@lists.postgresql.org>
Subject: Re: How to handle CASE statement with PostgreSQL without need for typecasting
Date: Tue, 18 Feb 2020 10:51:46 -0800
Message-ID: <CAPhAwGwzJ25Dp42RVgSAO2GCSx9eqQhPjuPcnJD5RGzdO+Ox7g@mail.gmail.com> (raw)
In-Reply-To: <CAFhuWu_vTQr0NSGiKSua8JPfHwGXSH_uQ6bzBWf_bRuKMu3a1g@mail.gmail.com>
References: <CAFhuWu_vTQr0NSGiKSua8JPfHwGXSH_uQ6bzBWf_bRuKMu3a1g@mail.gmail.com>
Good Morning,
>
> NOTE: From my research online, I found that typecasting works and also
> the error from the database suggests typecasting.
>
> This statement works:
>
> UPDATE t_update SET F1 = (CASE WHEN (?::timestamp(6) IS NULL ) THEN
> (?::timestamp(6) ) ELSE (?::timestamp(6) ) END)
>
>
There is no option to convert the text parameter to a timestamp - you need
to cast it - or use a parsing function or something else - but a text value
cannot drop directly into a timestamp column. But it's not the case
statement that is the issue - but rather the update - so you could shorten
the statement a little with this.
UPDATE t_update SET F1 = (CASE WHEN (? IS NULL ) THEN (?) ) ELSE (?) ) END)
::timestamp(6)
You don't need a timestamp until you place in in the column.
You also probably don't want a case statement here - not the standard
option for this
UPDATE t_update SET F1 = COALESCE(?, ?)::timestamp(6) with the first ?
being the when above and the second being the else above. See here [1]
John
[1] - https://www.postgresql.org/docs/current/functions-conditional.html>
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: wishdev@gmail.com
Subject: Re: How to handle CASE statement with PostgreSQL without need for typecasting
In-Reply-To: <CAPhAwGwzJ25Dp42RVgSAO2GCSx9eqQhPjuPcnJD5RGzdO+Ox7g@mail.gmail.com>
References: <CAFhuWu_vTQr0NSGiKSua8JPfHwGXSH_uQ6bzBWf_bRuKMu3a1g@mail.gmail.com> <CAPhAwGwzJ25Dp42RVgSAO2GCSx9eqQhPjuPcnJD5RGzdO+Ox7g@mail.gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox