Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 4892C1337BA5 for ; Sat, 8 Jan 2011 06:22:46 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 10967-02 for ; Sat, 8 Jan 2011 10:22:39 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from smtpcorp.com (smtpcorp.com [174.143.60.32]) by mail.postgresql.org (Postfix) with ESMTP id D0B901337B96 for ; Sat, 8 Jan 2011 06:22:38 -0400 (AST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=smtpcorp.com; s=main; h=Content-Transfer-Encoding:Content-Type:Mime-Version:References:Message-ID:Date:Subject:From:To; bh=cuZNa3HeuCx1ItunFplvbUZur0gkp1YkOG3Hl9H+6wU=; b=J22wXKcHGart4itbHUUEG3Q8/cPjbN5EWK/uDli1LSSJ+1Da7HW8fM4g+Hh/5+R7FpDcPvUyPl2gn5AtQXD7TSqcrxSrObZD1jVb7IKi+5jMEqpWDNflVCqMGfxxHJFN; Received: from ip-118-90-129-156.xdsl.xnet.co.nz ([118.90.129.156] helo=gonzo) by smtpcorp.com with esmtpsa (TLSv1:AES256-SHA:256) (Exim 4.70) id 1PbVwS-0002Ir-9j; mid 144 for pgsql-sql@postgresql.org; Sat, 08 Jan 2011 10:22:36 +0000 Received: from news by gonzo with local (Exim 4.72) (envelope-from ) id 1PbVwF-0002OD-MG for pgsql-sql@postgresql.org; Sat, 08 Jan 2011 23:22:23 +1300 To: pgsql-sql@postgresql.org Path: not-for-mail From: Jasen Betts Newsgroups: gmane.comp.db.postgresql.sql Subject: Re: pattern matching with dates? Date: 8 Jan 2011 10:22:23 GMT Organization: Dis (not Dat) Organisation Lines: 42 Message-ID: References: NNTP-Posting-Host: localhost Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Trace: reversiblemaps.ath.cx 1294482143 9187 127.0.0.1 (8 Jan 2011 10:22:23 GMT) 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-Smtpcorp-Track: 20122545.1.9946152 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-5 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201101/21 X-Sequence-Number: 34968 On 2011-01-05, Good, Thomas wrote: > This dubious query worked well previously: > select * from db_log where log_date LIKE '2011-01-%'; > (currently works on bluehost.com where they run 8.1.22) > > Can someone offer a (preferably ANSI compliant) way to do this on 8.4.5? where date_trunc( log_date,'month') = '2011-01-01'::timestamp; or standard where extract( 'year' from log_date) = 2011 and extract ( 'month' from log_date) = 1; this: where cast( log_date as varchar ) like '2011-01-%' is as far as I can tell standard, but is almost certainly non-portable as it is dependant of the character format used for casting dates to varchar. > I realize that >= and so on work well (which may explain why the docs > are pretty silent about pattern matching with dates) but sometimes it's nice to > treat the (ISO) date as a string. ">= etc" will outperform date_trunc, like , and extract if the date column is indexed. the performance of % can be improved in recent versions by indexing on the expression (log_date::text) best performance is probably where log_date between '2011-01-01'::date and '2011-01-01'::date + '1 month - 1 day' ::interval; or standard (I think) where log_date between cast('2011-01-01' as date) and cast ( '2011-01-01' as date) + cast ( '1 month - 1 day' as interval) ; -- ⚂⚃ 100% natural