X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id E211ED80D0 for ; Wed, 12 Oct 2005 19:01:24 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 48144-07 for ; Wed, 12 Oct 2005 22:01:18 +0000 (GMT) Received: from stark.xeocode.com (stark.xeocode.com [216.58.44.227]) by svr1.postgresql.org (Postfix) with ESMTP id 5552ED80CD for ; Wed, 12 Oct 2005 19:01:20 -0300 (ADT) Received: from localhost ([127.0.0.1] helo=stark.xeocode.com) by stark.xeocode.com with smtp (Exim 3.36 #1 (Debian)) id 1EPof1-0000Jf-00; Wed, 12 Oct 2005 18:01:19 -0400 To: Collin Peters Cc: pgsql-sql@postgresql.org Subject: Re: Text->Date conversion in a WHERE clause References: In-Reply-To: From: Greg Stark Organization: The Emacs Conspiracy; member since 1992 Date: 12 Oct 2005 18:01:18 -0400 Message-ID: <8764s21jkh.fsf@stark.xeocode.com> Lines: 37 User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.4 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.011 required=5 tests=[AWL=0.011] X-Spam-Level: X-Archive-Number: 200510/110 X-Sequence-Number: 22963 Collin Peters writes: > I have a table that has some columns which store 'custom' fields so the > content varies according to the user that the row belongs to. For one > of the groups of users the field is a date (the type of the field is > 'text' though). I'm trying to perform a query where it only returns > values in a certain date range so in the WHERE clause I have > > WHERE cust3 <> '' > AND cust3::text::timestamp > CURRENT_DATE - interval '1 month' > > This results in the error 'ERROR: date/time field value out of range: > This results in the error 'ERROR: date/time field value out of range: > "052-44-5863"'. Now that is obviously not a valid date.... but there > is actually more to the where clause and the first part of it excludes > all rows where the user is not even the correct type, so the row which > includes the field '052-44-5863' should really not even be checked. I think you have to use a CASE expression like: WHERE CASE WHEN user_type = 1 THEN cust3::timestamp > CURRENT_DATE - interval '1 month' ELSE false END CASE AND ... There's no advantage to doing this kind of thing though. Good database design principles dictate having one column for each piece of data. Just leave the columns for which the data is inappropriate NULL. NULLs take effectively no space. -- greg