2015-11-20 36 views
0

我有一個數據庫,我想從中提取生日在未來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天即將到來的生日(說)的列表...

+0

[SO Post](http://stackoverflow.com/questions/5459287/find-the-next-month-for-given-date-in-mysql)認爲它回答你的問題 –

+1

可能的重複[SQL Select即將到來的生日](http://stackoverflow.com/questions/83531/sql-select-upcoming-birthdays) – Filburt

+0

它不工作... –

回答

0

試試這個。 Add 15天在current date並從它得到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 values一個current month,一個在當前日期加上15 days後,所以如果month發生了變化,你會得到month的價值好。

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。

相關問題