How to handle CASE statement with PostgreSQL without need for typecasting - Mailing list pgsql-general

From Anthony Hall
Subject How to handle CASE statement with PostgreSQL without need for typecasting
Date
Msg-id CAFhuWu_vTQr0NSGiKSua8JPfHwGXSH_uQ6bzBWf_bRuKMu3a1g@mail.gmail.com
Whole thread Raw
Responses Re: How to handle CASE statement with PostgreSQL without need for typecasting  (John W Higgins <wishdev@gmail.com>)
List 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

pgsql-general by date:

Previous
From: Merlin Moncure
Date:
Subject: Re: DB running out of memory issues after upgrade
Next
From: John W Higgins
Date:
Subject: Re: How to handle CASE statement with PostgreSQL without need for typecasting