pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: PegoraroF10 <marcos@f10.com.br>
Cc: pgsql-general@postgresql.org
Subject: Re: Bug on version 12 ?
Date: Fri, 15 May 2020 12:27:40 -0400
Message-ID: <12005.1589560060@sss.pgh.pa.us> (raw)
In-Reply-To: <1589555314264-0.post@n3.nabble.com>
References: <1589555314264-0.post@n3.nabble.com>

PegoraroF10 <marcos@f10.com.br> writes:
> select 
>   JS ->> 'mydate'::text, 
>   to_timestamp((JS ->> 'mydate'), 'YYYY-MM-DD HH24:MI:SS')
> from (select '{"somefield": true, "otherfield": true, "mydate":
> "2020-04-02T18:26:50.941531-03:00"}'::jsonb) j(JS)

> This SQL works fine on 11.7 but not on 12.3 version.

Stripping away the JSON frippery, what you have is

to_timestamp('2020-04-02T18:26:50.941531-03:00', 'YYYY-MM-DD HH24:MI:SS')

which used to be allowed, but v12 is pickier; it insists that you account
for the "T" explicitly:

regression=# select to_timestamp('2020-04-02T18:26:50.941531-03:00', 'YYYY-MM-DD HH24:MI:SS');
ERROR:  invalid value "T1" for "HH24"
DETAIL:  Value must be an integer.
regression=# select to_timestamp('2020-04-02T18:26:50.941531-03:00', 'YYYY-MM-DDTHH24:MI:SS');
      to_timestamp      
------------------------
 2020-04-02 00:06:50-04
(1 row)

I think you're doing it wrong and you should just cast to timestamp:

regression=# select 
  JS ->> 'mydate'::text, 
  (JS ->> 'mydate')::timestamptz
from (select '{"somefield": true, "otherfield": true, "mydate":
"2020-04-02T18:26:50.941531-03:00"}'::jsonb) j(JS)
;
             ?column?             |          timestamptz          
----------------------------------+-------------------------------
 2020-04-02T18:26:50.941531-03:00 | 2020-04-02 17:26:50.941531-04
(1 row)

Use of to_timestamp() to parse a bog-standard time format is a
classic antipattern IMO.  It is inflexible, it doesn't detect
the errors you'd actually like it to detect, and it is harder
to type --- so why do people insist on doing it like that?

			regards, tom lane





view thread (13+ messages)  latest in thread

Message-ID: <12005.1589560060@sss.pgh.pa.us>
Permalink:  ../12005.1589560060@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/12005.1589560060@sss.pgh.pa.us

 · 

reply

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: tgl@sss.pgh.pa.us, marcos@f10.com.br
  Subject: Re: Bug on version 12 ?
  In-Reply-To: <12005.1589560060@sss.pgh.pa.us>
  References: <1589555314264-0.post@n3.nabble.com> <12005.1589560060@sss.pgh.pa.us>

* 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