pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Month/year between two dates
4+ messages / 4 participants
[nested] [flat]

* Month/year between two dates
@ 2009-08-11 13:13 Bor <dborovnik@gmail.com>
  2009-08-11 22:32 ` Re: Month/year between two dates Steve Crawford <scrawford@pinpointresearch.com>
  2009-08-12 03:59 ` Re: Month/year between two dates ramasubramanian <ramasubramanian.g@renaissance-it.com>
  0 siblings, 2 replies; 4+ messages in thread

From: Bor @ 2009-08-11 13:13 UTC (permalink / raw)
  To: pgsql-sql


Hi to all,

I have a very simple question. Let's say that I have three records (id, date
from, date to):

1     2009-01-01     2009-08-31
2     2009-08-01     2009-08-10
3     2009-08-11     2009-08-31

Now I want to get records, "related" to a single month/year data (two
integers). For 2009/08 (int1 = 2009, int2 = 8) I should get all three
records, for 2009/05 only record 1, but for 2009/11 none of the records.

Is there any simple way to do this? A query would do :).

Thanks alot.
-- 
View this message in context: http://www.nabble.com/Month-year-between-two-dates-tp24917400p24917400.html
Sent from the PostgreSQL - sql mailing list archive at Nabble.com.




^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Month/year between two dates
  2009-08-11 13:13 Month/year between two dates Bor <dborovnik@gmail.com>
@ 2009-08-11 22:32 ` Steve Crawford <scrawford@pinpointresearch.com>
  1 sibling, 0 replies; 4+ messages in thread

From: Steve Crawford @ 2009-08-11 22:32 UTC (permalink / raw)
  To: Bor <dborovnik@gmail.com>; +Cc: pgsql-sql

Bor wrote:
> Hi to all,
>
> I have a very simple question. Let's say that I have three records (id, date
> from, date to):
>
> 1     2009-01-01     2009-08-31
> 2     2009-08-01     2009-08-10
> 3     2009-08-11     2009-08-31
>
> Now I want to get records, "related" to a single month/year data (two
> integers). For 2009/08 (int1 = 2009, int2 = 8) I should get all three
> records, for 2009/05 only record 1, but for 2009/11 none of the records.
>
> Is there any simple way to do this? A query would do :).
>
> Thanks alot.
>   
Lots of ways. The following springs to mind but I'm sure there are 
simpler ways (I'm assuming the date_from and date_to are data-type date 
and you are stuck with using int for year and month).

Use date_trunc to convert any date in a month to the first of the month 
and the following should work (untested):
...
date_trunc('month', date_from) <= (int1::text || '-' || int2::text || 
'-1')::date and
date_trunc('month', date_to) >= (int1::text || '-' || int2::text || 
'-1')::date
...

Cheers,
Steve



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Month/year between two dates
  2009-08-11 13:13 Month/year between two dates Bor <dborovnik@gmail.com>
@ 2009-08-12 03:59 ` ramasubramanian <ramasubramanian.g@renaissance-it.com>
  2009-08-13 00:58   ` Re: Month/year between two dates Jorge Godoy <jgodoy@gmail.com>
  1 sibling, 1 reply; 4+ messages in thread

From: ramasubramanian @ 2009-08-12 03:59 UTC (permalink / raw)
  To: Bor <dborovnik@gmail.com>; pgsql-sql

Dear Bor,
    How you will 1 record for 2009/05 (if you use 2009/05 ) it will fetch 
all the records as it is not having month 05
am i correct?

----- Original Message ----- 
From: "Bor" <dborovnik@gmail.com>
To: <pgsql-sql@postgresql.org>
Sent: Tuesday, August 11, 2009 6:43 PM
Subject: [SQL] Month/year between two dates


>
> Hi to all,
>
> I have a very simple question. Let's say that I have three records (id, 
> date
> from, date to):
>
> 1     2009-01-01     2009-08-31
> 2     2009-08-01     2009-08-10
> 3     2009-08-11     2009-08-31
>
> Now I want to get records, "related" to a single month/year data (two
> integers). For 2009/08 (int1 = 2009, int2 = 8) I should get all three
> records, for 2009/05 only record 1, but for 2009/11 none of the records.
>
> Is there any simple way to do this? A query would do :).
>
> Thanks alot.
> -- 
> View this message in context: 
> http://www.nabble.com/Month-year-between-two-dates-tp24917400p24917400.html
> Sent from the PostgreSQL - sql mailing list archive at Nabble.com.
>
>
> -- 
> Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-sql 




^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Month/year between two dates
  2009-08-11 13:13 Month/year between two dates Bor <dborovnik@gmail.com>
  2009-08-12 03:59 ` Re: Month/year between two dates ramasubramanian <ramasubramanian.g@renaissance-it.com>
@ 2009-08-13 00:58   ` Jorge Godoy <jgodoy@gmail.com>
  0 siblings, 0 replies; 4+ messages in thread

From: Jorge Godoy @ 2009-08-13 00:58 UTC (permalink / raw)
  To: ramasubramanian <ramasubramanian.g@renaissance-it.com>; Bor <dborovnik@gmail.com>; pgsql-sql

I think he wanted something like:

test=# create table month_test(id serial primary key, start_date date not
null, end_date date not null);
CREATE TABLE
test=# insert into month_test (start_date, end_date) values
('2009-01-01'::date, '2009-08-31'::date);
INSERT 0 1
test=# insert into month_test (start_date, end_date) values
('2009-08-01'::date, '2009-08-10'::date);
INSERT 0 1
test=# insert into month_test (start_date, end_date) values
('2009-08-11'::date, '2009-08-31'::date);
INSERT 0 1
test=# select * from month_test;
 id | start_date |  end_date
----+------------+------------
  1 | 2009-01-01 | 2009-08-31
  2 | 2009-08-01 | 2009-08-10
  3 | 2009-08-11 | 2009-08-31
(3 rows)

test=# select * from month_test where (start_date, end_date) overlaps
('2009-05-01'::date, '2009-05-31'::date);
 id | start_date |  end_date
----+------------+------------
  1 | 2009-01-01 | 2009-08-31
(1 row)

test=#

--
Jorge Godoy     <jgodoy@gmail.com>


On Wed, Aug 12, 2009 at 00:59, ramasubramanian <
ramasubramanian.g@renaissance-it.com> wrote:

> Dear Bor,
>   How you will 1 record for 2009/05 (if you use 2009/05 ) it will fetch all
> the records as it is not having month 05
> am i correct?
>
> ----- Original Message ----- From: "Bor" <dborovnik@gmail.com>
> To: <pgsql-sql@postgresql.org>
> Sent: Tuesday, August 11, 2009 6:43 PM
> Subject: [SQL] Month/year between two dates
>
>
>
>> Hi to all,
>>
>> I have a very simple question. Let's say that I have three records (id,
>> date
>> from, date to):
>>
>> 1     2009-01-01     2009-08-31
>> 2     2009-08-01     2009-08-10
>> 3     2009-08-11     2009-08-31
>>
>> Now I want to get records, "related" to a single month/year data (two
>> integers). For 2009/08 (int1 = 2009, int2 = 8) I should get all three
>> records, for 2009/05 only record 1, but for 2009/11 none of the records.
>>
>> Is there any simple way to do this? A query would do :).
>>
>> Thanks alot.
>> --
>> View this message in context:
>> http://www.nabble.com/Month-year-between-two-dates-tp24917400p24917400.html
>> Sent from the PostgreSQL - sql mailing list archive at Nabble.com.
>>
>>
>> --
>> Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
>> To make changes to your subscription:
>> http://www.postgresql.org/mailpref/pgsql-sql
>>
>
>
> --
> Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-sql
>

^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2009-08-13 00:58 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2009-08-11 13:13 Month/year between two dates Bor <dborovnik@gmail.com>
2009-08-11 22:32 ` Steve Crawford <scrawford@pinpointresearch.com>
2009-08-12 03:59 ` ramasubramanian <ramasubramanian.g@renaissance-it.com>
2009-08-13 00:58   ` Jorge Godoy <jgodoy@gmail.com>

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