Discussion:
Whose birthday is coming up
(too old to reply)
Your Name
2007-01-26 01:43:46 UTC
Permalink
I have an SQL problem I have been struggling with. Given birthdates
stored in a date format, I want to be able to pull out all those whose
birthday is between today and a week from now. This is quite tricky,
unless I'm missing something, because I somehow have to drop the year
and compare the month and day only which could span into the next month
or even next year. But I studied the date/time functions and not even a
combinations of functions seems to do the trick. I've pretty much given
up except for this post. If someone could offer some clever code to do
this, it would make my week!

Thanks for the help in advance.

Mark
Your Name
2007-01-26 02:13:15 UTC
Permalink
Post by Your Name
I have an SQL problem I have been struggling with. Given birthdates
stored in a date format, I want to be able to pull out all those whose
birthday is between today and a week from now. This is quite tricky,
I may have answered my own question with a breakthrough of my own code
(in the WHERE clause):

select p.*
from
Student as s inner join personData as p on p.pid = s.self
where
str_to_date(date_format(s.birthdate,"%m-%d"),"%m-%d")
+ interval 0 day >=
str_to_date(date_format(now(),"%m-%d"),"%m-%d")
+ interval 0 day
and
str_to_date(date_format(s.birthdate,"%m-%d"),"%m-%d")
+ interval 0 day <
str_to_date(date_format(now(),"%m-%d"),"%m-%d")
+ interval 7 day;

Mark
Carl Pearson
2007-01-27 02:07:46 UTC
Permalink
Post by Your Name
Post by Your Name
I have an SQL problem I have been struggling with. Given birthdates
stored in a date format, I want to be able to pull out all those whose
birthday is between today and a week from now. This is quite tricky,
I may have answered my own question with a breakthrough of my own code
select p.*
from
Student as s inner join personData as p on p.pid = s.self
where
str_to_date(date_format(s.birthdate,"%m-%d"),"%m-%d")
+ interval 0 day >=
str_to_date(date_format(now(),"%m-%d"),"%m-%d")
+ interval 0 day
and
str_to_date(date_format(s.birthdate,"%m-%d"),"%m-%d")
+ interval 0 day <
str_to_date(date_format(now(),"%m-%d"),"%m-%d")
+ interval 7 day;
Mark
Check out "adddate" (a synonym for date_add) in the manual:
http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html
Loading...