Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts

    Post Bloated birthday query-- is there a better way?

    I set out writing a query that determines birthdays coming up in the next 2 months. I have stored the original birth date, so I had to plug in the current year for the comparison, but then in december I wouldn't get january's birthdays, so I added an if statement to plug in current year or next year depending on if the birthday has already passed for the year. Anyway, I ended up with this:

    PHP Code:
    $query sprintf("
    SELECT people_nm,
    date_format(birth_dt, '%%b %%D') as birth_dt_f,

    date_format(current_date, '%%Y') - date_format(birth_dt, '%%Y') as age_nr,

    if (concat(year(current_timestamp), '-', date_format(birth_dt, '%%m'), '-', date_format(birth_dt, '%%d')) >= current_date,
        concat(year(current_timestamp), '-', date_format(birth_dt, '%%m'), '-', date_format(birth_dt, '%%d')),
        concat(year(current_timestamp  + interval 1 year), '-', date_format(birth_dt, '%%m'), '-', date_format(birth_dt, '%%d'))) as birthday

    FROM people_tbl

    WHERE if (concat(year(current_timestamp), '-', date_format(birth_dt, '%%m'), '-', date_format(birth_dt, '%%d')) >= current_date,
        concat(year(current_timestamp), '-', date_format(birth_dt, '%%m'), '-', date_format(birth_dt, '%%d')),
        concat(year(current_timestamp  + interval 1 year), '-', date_format(birth_dt, '%%m'), '-', date_format(birth_dt, '%%d')))
    between current_date and (current_date + interval 2 month)

    ORDER BY birthday
    "
    ); 
    Which works, but it's so bloated I wonder if there's a better way?

  • #2
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    Try:

    SELECT people_nm WHERE birth_dt between CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 2 MONTH);

  • #3
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    I wish I could do that, but the birthdays stored are the actual days of birth, i.e. 1969-02-07, so I have to convert the year to current year (or current year + 1 if the birthday has already happened in current year).

  • #4
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,169
    Thanks
    19
    Thanked 65 Times in 64 Posts
    wow thats a gnarly query

    Hmmm, this is completely off the top of my head so it might not work but it cant hurt to try heh

    PHP Code:
    $now date("m-d");
    $then date("m-d"strtotime("+ 2 months"));

    $sql "SELECT people_nm WHERE date_format(birth_dt, '%m-%d') between '$now' and '$then'"
    or in fact could you do

    Code:
    SELECT people_nm WHERE date_format(birth_dt, '%m-%d) BETWEEN date_format(CURDATE(), '%m-%d') AND date_format(DATE_ADD(CURDATE(), INTERVAL 2 MONTH), '%m-%d');
    ...probably wont work but its worth a shot?

  • #5
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,169
    Thanks
    19
    Thanked 65 Times in 64 Posts
    ahhh just thought - that doesnt account for the year crossover.

    Herm, if your query works then theres no need to change it right?

  • #6
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    how about:

    DATEDIFF(birth_dt, CURDATE())%365

    Sure, that's off by one day for each leap year, but that means only people older than 7 will notice!!!

    Ok, so no dice there.

    You could try: MONTH(birth_dt) BETWEEN MONTH(CURDATE()) AND MONTH(CURDATE())+2

  • #7
    Super Moderator guelphdad's Avatar
    Join Date
    Mar 2006
    Location
    St. Catharines, Ontario Canada
    Posts
    2,631
    Thanks
    4
    Thanked 147 Times in 138 Posts
    This will get birthdays next month and the month after:

    Code:
    select
    columna,
    columnb,
    columnc
    from yourtable
    where
    month(birthdaycolumn) IN
    (
    month(date_add(curdate, interval 1 month)),
    month(date_add(curdate, interval 2 month)),
    )
    so those would be October and November birthdays even if you were calculating on September 1st or September 30th.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •