20

Hello All I have a table in my pg admin database.There is an employee table in this table.Having the field:- 1)name 2)date_of_birth

Now the scenario is that I want to know the birth day for current date and upcoming 20 days For example if current date is 28-Jan-2013 then

1)from_date=28-Jan-2013
2)to_date=16-feb-2013

I want to select all the records from the table for which the date_of_birth

lies between 28-Jan and 16-feb
1
  • Is important to you that the year was hide? Commented Jan 29, 2013 at 19:21

3 Answers 3

40

Try this:

SELECT *
FROM   bdaytable
WHERE  bdate >= '2013-01-28'::DATE
AND    bdate <= '2013-02-16'::DATE;

You may also try overlaps:

SELECT *
FROM bdaytable
WHERE (bdate, bdate) 
OVERLAPS ('2013-01-28'::DATE, '2013-02-16'::DATE);

with extract, month, day:

SELECT *
FROM   bdaytable
WHERE  Extract(month from bdate) >= Extract(month from '2013-01-28'::DATE)
AND    Extract(month from bdate) <= Extract(month from '2013-02-16'::DATE)
AND    Extract(day from bdate) >= Extract(day from '2013-01-28'::DATE)
AND    Extract(day from bdate) <= Extract(day from '2013-02-16'::DATE);

Incorporating Now() and interval to make the query dynamic with current date:

SELECT *
FROM   bdaytable
WHERE  Extract(month from bdate) >= Extract(month from Now())
AND    Extract(month from bdate) <= Extract(month from Now() + Interval '20 day')
AND    Extract(day from bdate) >= Extract(day from Now())
AND    Extract(day from bdate) <= Extract(day from Now() + Interval '20 day');
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks dear.But in my case year doesn't matter I want to consider only date and month
Try with the edit please :), it's ugly with too many extracts, but should work.
I think conditions are not used properly
Absolutely , good catch, let me know if it's working for you, keen to know ;)
Another way to do this - (bdate - date_trunc('year',bdate)) BETWEEN (now() - date_trunc('year',now())) AND (now() - date_trunc('year',now()) + interval '20 days'). The idea - to compare intervals from the start of the year.
|
4
select *
from employee
where 
    to_char(date_of_birth, 'MMDD') between 
    to_char(current_date, 'MMDD') and to_char(current_date + 20, 'MMDD')

5 Comments

Will it work, when the current date is close (less than 20 days) to the end of the year?
Thanks this is working fine.But if the current_date is 20-Nov-2012 and to_date is 15-Jan-2013 and I want the result in this order :-birth day for Nov,birth day for DEC and birth day for Jan .How can I achieve this
@facebook Don't use this answer. It is wrong as already pointed by Igor
@Clodoaldo I have modify this to work for all case.I have made 2 range of dates.For example if the date is 20-Dec-2012.Then Dates are:-20-Dec-2012,31-Dec-2012 AND 1-Jan-2013 to 8-Jan-2013
@facebook Interesting. Could you please post a new question explaining your solution in detail and then asking for the order problem?
0

I think this should work. Use interval.

SELECT * 
FROM Employee
WHERE Date_Of_Birth >= now() AND Date_Of_Birth <= now() + interval '20 day'

If you want to get any birth date between these days (and year doesn't matter), then that would be slightly different.

EDIT

If year doesn't matter, while I'm sure there is a better way, this could work. Basically it's just converting all years to a common year (in this case 2000) -- you could do the same with your input parameter as well.

(Date_Of_Birth + (2000-EXTRACT(YEAR FROM Date_Of_Birth) || ' years')::interval)

Curious what others have used in the past as this is probably not the most efficient.

Good luck.

1 Comment

Hi,As you mention what if year doesn't matter.In my case year does'nt matter

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.