0

I am having a database, from which I want to fetch list of people whose birthdays are in coming 15 days. I'm using ASP.NET C# and MS-Access as Database. I googled a lot but Couldn't find correct explanation. I'm using

select name,category,dob 
from family_details 
where Month(dob) >= Month(NOW()) and Day(dob) > Day(Now()) 
order by dob desc

this query, which gives list of Birthdays in Current months from today's date. The result I want is the list of upcoming Birthday(say) of 15 days...

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364

2 Answers2

0

Try this. Add 15 days in current date and get month from it.

select name,category,dob 
from family_details 
where Month(dob) in(Month(NOW()),Month(DateAdd(d,15,NOW()))) 
order by dob desc

Month can change in next 15 days so you also need to get that month .Here you will get two values one for current month and one after adding 15 days in current date so if month has changed you will get value of that month as well.

Mairaj Ahmad
  • 14,434
  • 2
  • 26
  • 40
0

You may need a helper function to get it right also for leaplings - those born on February 29th. The trick is to use AddYears which doesn't fail for leap years.

It can be created as an extension method of DateTime:

/// <summary>
/// Calculates the next annual day of this instance of System.DateTime relative to today.
/// <para>Calculates correctly Feb. 28th as an annual day in common years for event dates of Feb. 29th.</para>
/// <para>If next annual day should be later than 9999-12-31, the annual day of year 9999 is returned.</para>
/// </summary>
/// <param name="eventDate">The date of the event.</param>
/// <returns>The date of the upcoming annual day.</returns>
public static DateTime NextAnnualDay(this DateTime eventDate)
{
    return NextAnnualDay(eventDate, DateTime.Today);
}

/// <summary>
/// Calculates the next annual day of this instance of System.DateTime relative to a future date.
/// <para>Calculates correctly Feb. 28th as an annual day in common years for event dates of Feb. 29th.</para>
/// <para>If futureDate is earlier than this instance, the value of this instance is returned.</para>
/// <para>If next annual day should be later than 9999-12-31, the annual day of year 9999 is returned.</para>
/// </summary>
/// <param name="eventDate">The date of the event.</param>
/// <param name="futureDate">The date from which to find the next annual day of this instance.</param>
/// <returns>The date of the upcoming annual day.</returns>
public static DateTime NextAnnualDay(this DateTime eventDate, DateTime futureDate)
{
    DateTime nextDate = eventDate;
    if (DateTime.MaxValue.Year - eventDate.Year == 0)
    {
        // No later next annual day can be calculated.
    }
    else
    {
        int years = futureDate.Year - eventDate.Year;
        if (years < 0)
        {
            // Don't calculate a hypothetical next annual day.
        }
        else
        {
            nextDate = eventDate.AddYears(years);
            if (nextDate.Subtract(futureDate).Days <= 0)
            {
                if (DateTime.MaxValue.Year - nextDate.Year == 0)
                {
                    // No later next annual day can be calculated.
                }
                else
                {
                    nextDate = eventDate.AddYears(years + 1);
                }
            }
        }
    }
    return nextDate;
}

Then go like this (pseudo SQL):

select name, category, dob 
from family_details 
where dob.NextAnnualDay() between today and today.AddDays(15) 
order by dob desc

Finish using Linq or transform to parameterized SQL.

Gustav
  • 53,498
  • 7
  • 29
  • 55