How to handle CASE statement with PostgreSQL without need for typecasting

From: Anthony Hall <eat(dot)drink(dot)be(dot)merry(at)gmail(dot)com>
To: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: How to handle CASE statement with PostgreSQL without need for typecasting
Date: 2020-02-18 18:40:58
Message-ID: CAFhuWu_vTQr0NSGiKSua8JPfHwGXSH_uQ6bzBWf_bRuKMu3a1g@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hello,

Trying to find a way to perform a CASE statement without needing to
typecast. Research so far suggests that this is not possible, but I wanted
to check with the PSQL community to be sure.

Steps:
1. CREATE TABLE t_update (F1 timestamp(6) NULL )

2. Run statement UPDATE t_update SET F1 = (CASE WHEN (CURRENT_TIMESTAMP
IS NULL ) THEN (CURRENT_TIMESTAMP ) ELSE (CURRENT_TIMESTAMP ) END)
Result: No exceptions.

3. Run parametrised statement
UPDATE t_update SET F1 = (CASE WHEN (? IS NULL ) THEN (? ) ELSE (? ) END)

Result: Error
ERROR: VERROR; column "f1" is of type timestamp without time zone but
expression is of type text(Hint You will need to rewrite or cast the
expression.; Position 27; File
d:\pginstaller.auto\postgres.windows-x64\src\backend\parser\parse_target.c;
Line 591; Routine transformAssignedExpr; ) (6822148)

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)

Please let me know your thoughts.

Many thanks!
Anthony

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Merlin Moncure 2020-02-18 18:49:50 Re: DB running out of memory issues after upgrade
Previous Message Justin Pryzby 2020-02-18 18:40:37 Re: DB running out of memory issues after upgrade