【问题标题】:How to Fetch Upcoming Birthdays from Database?如何从数据库中获取即将到来的生日?
【发布时间】:2016-02-22 16:40:55
【问题描述】:

我有一个数据库,我想从中获取未来 15 天内生日的人的列表。我使用 ASP.NET C# 和 MS-Access 作为数据库。 我用谷歌搜索了很多,但找不到正确的解释。 我正在使用

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

这个查询,它给出了从今天开始的当前月份的生日列表。 我想要的结果是即将到来的 15 天生日(比如说)的列表......

【问题讨论】:

  • SO Post 认为它回答了你的问题
  • 它不工作...
  • @Filburt 为什么使用 SQL Server 回答 MS Access SQL 问题?
  • @Filburt 它不是那个问题的重复。

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


【解决方案1】:

试试这个。 Addcurrent 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 values 一个用于current month,另一个是在当前日期添加15 days 之后,如果month已更改,您也将获得 month 的值。

【讨论】:

    【解决方案2】:

    您可能需要一个辅助函数来为跳跃者(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。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-03
      • 1970-01-01
      • 2014-05-03
      • 2017-06-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多