From: Jasen Betts <jasen@xnet.co.nz>
To: pgsql-sql@postgresql.org
Subject: Re: to_char() accepting invalid dates?
Date: 18 Jul 2011 09:23:27 GMT
Message-ID: <j00u2f$rg8$3@reversiblemaps.ath.cx> (raw)
References: <j00qg6$lvf$1@dough.gmane.org>
On 2011-07-18, Thomas Kellerer <spam_eater@gmx.net> wrote:
> Hi,
>
> I just noticed that to_char() will "accept" invalid dates such as 2011-02-31 and "adjust" them accordingly:
>
> postgres=> select to_date('20110231', 'yyyymmdd');
>
> to_date
> ------------
> 2011-03-03
> (1 row)
>
> is there a way to have to_date() raise an exception in such a case?
it's possible the odd behaviour you get is required by some standard.
however 'yyyymmdd' is a format that postgres understands natively,
so just cast the string to date.
jasen=# select '20110303'::date;
date
------------
2011-03-03
(1 row)
jasen=# select '20110231'::date;
ERROR: date/time field value out of range: "20110231"
LINE 1: select '20110231'::date;
^
postgres also understands YYYY-MM-DD and possilby a locale
dependant form with the year last
--
⚂⚃ 100% natural
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 using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: jasen@xnet.co.nz
Subject: Re: to_char() accepting invalid dates?
In-Reply-To: <j00u2f$rg8$3@reversiblemaps.ath.cx>
* 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