Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 764C6B5DC00 for ; Mon, 18 Jul 2011 06:30:34 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 31560-02 for ; Mon, 18 Jul 2011 09:30:28 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from lo.gmane.org (lo.gmane.org [80.91.229.12]) by mail.postgresql.org (Postfix) with ESMTP id EB750B5DC04 for ; Mon, 18 Jul 2011 06:30:24 -0300 (ADT) Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1Qik9e-0004YD-Ob for pgsql-sql@postgresql.org; Mon, 18 Jul 2011 11:30:22 +0200 Received: from ip-118-90-106-232.xdsl.xnet.co.nz ([118.90.106.232]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 18 Jul 2011 11:30:22 +0200 Received: from jasen by ip-118-90-106-232.xdsl.xnet.co.nz with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 18 Jul 2011 11:30:22 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-sql@postgresql.org From: Jasen Betts Subject: Re: to_char() accepting invalid dates? Date: 18 Jul 2011 09:23:27 GMT Organization: Dis (not Dat) Organisation Lines: 34 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: ip-118-90-106-232.xdsl.xnet.co.nz X-Face: ?)Aw4rXwN5u0~$nqKj`xPz>xHCwgi^q+^?Ri*+R(&uv2=E1Q0Zk(>h!~o2ID@6{uf8s; a +M[5[U[QT7xFN%^gR"=tuJw%TXXR'Fp~W; (T"1(739R%m0Yyyv*gkGoPA.$b,D.w:z+<'"=-lVT?6 {T?=R^:W5g|E2#EhjKCa+nt":4b}dU7GYB*HBxn&Td$@f%.kl^:7X8rQWd[NTc"P"u6nkisze/Q; 8 "9Z{peQF,w)7UjV$c|RO/mQW/NMgWfr5*$-Z%u46"/00mx-,\R'fLPe.)^ User-Agent: slrn/pre1.0.0-18 (Linux) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-3.013 tagged_above=-5 required=5 tests=BAYES_00=-1.9, RP_MATCHES_RCVD=-1.113 X-Spam-Level: X-Archive-Number: 201107/79 X-Sequence-Number: 35649 On 2011-07-18, Thomas Kellerer 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