Re: Extract between year *and* month

From: Russell Smith <mr-russ(at)pws(dot)com(dot)au>
To: One <david(dot)hunter(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Extract between year *and* month
Date: 2006-11-21 10:04:27
Message-ID: 4562CF2B.4050406@pws.com.au
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

One wrote:
> Hi - I'd like to return search results based on a date range that is
> selected by the user :
>
> <select name="date1yy">
> <option value="1995-01-01" selected>1995</option>
>
> <select name="date2yy">
> <option value="2006-12-31" selected>2006</option>
>
> My SQL is :
>
> select *
> from tablename
> where title like ('%nith%')
> and recall_date between
> extract(year from date '1995-01-01')
> and extract(year from date '2006-12-31')
> order by recall_date
>
> How can I add the MONTH to be included in the between statement ?
> If I select 1995 - 2006 it currently only returns rows up to 2005-12-31
>
> THANKS!
>
Try

select *
from tablename
where title like ('%nith%')
and date_trunc('month',recall_date::timestamp) >= date_trunc('month','1995-01-01'::timestamp)
and date_trunc('month',recall_date::timestamp) <= date_trunc('month','2006-12-31'::timestamp)
order by recall_date

>
> ---------------------------(end of broadcast)---------------------------
> TIP 5: don't forget to increase your free space map settings
>
>
>

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Desmond Coughlan 2006-11-21 10:18:15 more on database design
Previous Message Dimitri Fontaine 2006-11-21 09:38:52 Re: PostgreSQL equivalent of the Oracale 'unique' qualifier