Re: Question on cast string to date

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: 正华吕 <kainwen(at)gmail(dot)com>
Cc: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: Question on cast string to date
Date: 2022-05-10 04:44:43
Message-ID: 4035765.1652157883@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

=?UTF-8?B?5q2j5Y2O5ZCV?= <kainwen(at)gmail(dot)com> writes:
> I test the following SQL in pg15dev (seems same behavior as the
> previous version).
> select '2020701'::date;
> date
> ------------
> 0202-07-01
> (1 row)
> At the first glance, the result seems quite strange.

[ shrug... ] You left out a zero. It's not apparent to me that
this answer is wrong.

> The question here is: should we throw error for such input? Or what
> standard postgres is
> using to cast such kind of string?

The bar to changing any behavior here is a *lot* higher than
you seem to imagine.

Having said that, it does appear that we changed this somewhere
between 9.3 and 9.4:

psql (9.3.25)
Type "help" for help.

regression=# select '2020701'::date;
ERROR: invalid input syntax for type date: "2020701"
LINE 1: select '2020701'::date;
^

psql (9.4.26)
Type "help" for help.

regression=# select '2020701'::date;
date
------------
0202-07-01
(1 row)

If you want to pursue this question, you could start by bisecting
to find just which commit changed it and why.

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message David G. Johnston 2022-05-10 05:40:58 Re: Question on cast string to date
Previous Message Ian Lawrence Barwick 2022-05-10 04:38:23 Re: Question on cast string to date