pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: gzh <gzhcoder@126.com>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: TO_DATE function between PostgreSQL 8.2 and 9.4
Date: Tue, 17 May 2022 12:21:55 -0400
Message-ID: <1590028.1652804515@sss.pgh.pa.us> (raw)
In-Reply-To: <35162944.7eee.180d2badd7c.Coremail.gzhcoder@126.com>
References: <35162944.7eee.180d2badd7c.Coremail.gzhcoder@126.com>

gzh <gzhcoder@126.com> writes:
> I run following sql in PostgreSQL 8.2, it return my expected result as ¡®YYYY-MM-DD¡¯ format.

> --PostgreSQL 8.2
> ---------------

> SELECT to_date(now() + '-7day', 'YYYY-MM-DD');

TBH, this was poor SQL code in 8.2, and it's poor SQL code now.
to_date doesn't take timestamp, and never has: it takes text.
The reason the query succeeded in 8.2 was that at the time, the
server would silently cast just about anything to text if necessary
to find a workable interpretation of the query.  We got rid of that
behavior in 8.3, because way too often the server's opinion of a
"workable interpretation" was surprising.

So you could make it work the way it used to by inserting the text
coercion explicitly:

=# SELECT to_date((now() + '-7day')::text, 'YYYY-MM-DD');
  to_date   
------------
 2022-05-10

However, this hasn't done anything to make the call safer or more
performant.  It's slow because of an unnecessary formatting and
deformatting of the text equivalent of the timestamp, and it's
unsafe because your format choice represents a hard-wired assumption
that DateStyle is ISO.  For example, if I do

=# set datestyle = postgres;
SET

which means that timestamps will render to text like

=# select (now() + '-7day');
              ?column?               
-------------------------------------
 Tue May 10 12:11:25.474873 2022 EDT
(1 row)

then it falls over completely:

=# SELECT to_date((now() + '-7day')::text, 'YYYY-MM-DD');
ERROR:  invalid value "Tue " for "YYYY"
DETAIL:  Value must be an integer.

You could get the equivalent behavior quicker and more safely by just
casting the timestamp value to date:

=# select (now() + '-7day')::date;
    date    
------------
 2022-05-10
(1 row)

You might also want to look at other non-text-based manipulations
such as date_trunc() and date_part().

			regards, tom lane





view thread (7+ messages)  latest in thread

Message-ID: <1590028.1652804515@sss.pgh.pa.us>
Permalink:  ../1590028.1652804515@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/1590028.1652804515@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, gzhcoder@126.com
  Subject: Re: TO_DATE function between PostgreSQL 8.2 and 9.4
  In-Reply-To: <1590028.1652804515@sss.pgh.pa.us>
  References: <35162944.7eee.180d2badd7c.Coremail.gzhcoder@126.com> <1590028.1652804515@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