如何从数据库中获取即将到来的生日?

时间:2015-11-20 08:55:57

标签: c# sql asp.net .net ms-access

我有一个数据库,我想从中获取生日即将到来15天的人员列表。我使用ASP.NET C#和MS-Access作为数据库。 我google了很多但无法找到正确的解释。 我正在使用

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

此查询,显示从今天开始的当前月份中的生日列表。 我想要的结果是即将到来的生日(比如说)15天的清单......

2 个答案:

答案 0 :(得分:0)

试试这个。 Add中的current date 15天,并从month获取select name,category,dob from family_details where Month(dob) in(Month(NOW()),Month(DateAdd(d,15,NOW()))) order by dob desc

Month

next 15 days可以在month中进行更改,因此您还需要获得two valuescurrent month一个15 daysmonth加一month {1}}在当前日期,如果The controller for path '/Admin/Widget/ConfigureWidget' was not found or does not implement IController. 发生了变化,您也会获得"Nop.Plugin.Widgets.Mywidget"的价值。

答案 1 :(得分:0)

你可能还需要一个辅助功能才能使它也适合2月29日出生的树苗。诀窍是使用AddYears,它不会因闰年而失败。

可以将其创建为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;
}

然后像这样(伪SQL):

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

使用Linq完成或转换为参数化SQL。