如何从数据库中获取即将到来的bithdays [英] How to fetch upcoming bithdays from database
本文介绍了如何从数据库中获取即将到来的bithdays的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用sql查询对出生日期进行排序以查找即将到来的生日。问题是它正在考虑所有三个,即日期,月份,年份,而我只需要使用日期和月份进行排序,这样我就可以获得明天的生日列表,后天或者等等......
提前致谢...
解决方案
看看这里: http://www.mombu.com/microsoft/sql-server/t-calculate-days-till-next-birthday-336649。 html [ ^ ]
那里的SQL代码返回下一个生日的天数,这对于您修改SQL应该很简单。
这是一个函数:
公共 功能 DateNextBirthday(_
ByVal BirthDate 作为 日期,_
可选 ByVal SomeDate As 变体)_
作为 日期
' 根据BirthDate计算SomeDate之后的下一个生日。
' 如果SomeDate早于BirthDate,则返回BirthDate。
' 如果下一个生日应该晚于9999-12-31,则
' 返回年份9999的生日。
'
' 2003-02-14。 Cactus Data ApS,CPH。
' 2015-11-21。如果BirthDate是2月29日,则更正闰年的计算。
' 重命名变量。
' 添加了对过多值的检查。
< span class =code-keyword> Const MaxDateValue As 日期 =#12/31 / 9999#
Dim NextBirthDate As 日期
Dim 年作为 整数
如果 不 IsDate(SomeDate)< span class =code-keyword>然后
' 空或inva盖子参数SomeDate。
' 今天使用SomeDate。
SomeDate = 日期
结束 如果
NextBirthDate = BirthDate
如果 DateDiff( yyyy,BirthDate,MaxDateValue)= 0 然后
' 不能计算以后的生日。
Else
年= DateDiff( yyyy,BirthDate ,SomeDate)
如果年< 0 然后
' < span class =code-comment>不计算假设的生日。
否则
NextBirthDate = DateAdd( yyyy,年,BirthDate)
如果 DateDiff( d,SomeDate,NextBirthDate)< = 0 然后
' 下一个生日在今年早些时候落在SomeDate之前。
如果 DateDiff( yyyy,NextBirthDate,MaxDateValue)= 0 然后
' 不能计算以后的生日。
其他
NextBirthDate = DateAdd( yyyy ,年+ 1 ,BirthDate)
结束 如果
结束 如果
结束 如果
结束 如果
DateNextBirthday = NextBirthDate
结束 功能
因此,要在Access中运行查询,您可以执行以下操作:
选择
Id,
FirstName,
LastName,
DateNextBirthday([DOB])作为 NextBirthday
来自
tblYourPersonTable
订单 按
DateNextBirthday([DOB])
请注意,这将返回正确的生日也适用于任何一年的leaplings(2月29日出生的人)。
尝试以下查询
选择 FirstName,LastName,DateofBirth 来自 TableName A
其中月(DateofBirth)> =月(GETDATE())和日(DateofBirth) > Day(GETDATE())
order by DateofBirth
I am using sql query to sort Date of Births as to find upcoming birthdays. The problem is that it is considering all three i.e, Date, Month, Year while I only need to sort using Date and Month so that I can have the list of Birthdays for Tomorrow, day after tomorrow or so...
Thanks in advance...
解决方案
Have a look here: http://www.mombu.com/microsoft/sql-server/t-calculate-days-till-next-birthday-336649.html[^]
The SQL code there returns the number of days until the next birthday, which should be simple for you to modify for your SQL.
Here is a function for exactly this:
Public Function DateNextBirthday( _ ByVal BirthDate As Date, _ Optional ByVal SomeDate As Variant) _ As Date ' Calculates next birthday following SomeDate based on BirthDate. ' If SomeDate is earlier than BirthDate, BirthDate is returned. ' If next birthday should be later than 9999-12-31, the ' birthday of year 9999 is returned. ' ' 2003-02-14. Cactus Data ApS, CPH. ' 2015-11-21. Corrected calculation for leap years if BirthDate is Feb. 29th. ' Variables renamed. ' Added check for excessive values. Const MaxDateValue As Date = #12/31/9999# Dim NextBirthDate As Date Dim Years As Integer If Not IsDate(SomeDate) Then ' Empty or invalid parameter SomeDate. ' Use today as SomeDate. SomeDate = Date End If NextBirthDate = BirthDate If DateDiff("yyyy", BirthDate, MaxDateValue) = 0 Then ' No later birthday can be calculated. Else Years = DateDiff("yyyy", BirthDate, SomeDate) If Years < 0 Then ' Don't calculate hypothetical birthdays. Else NextBirthDate = DateAdd("yyyy", Years, BirthDate) If DateDiff("d", SomeDate, NextBirthDate) <= 0 Then ' Next birthday falls earlier in the year than SomeDate. If DateDiff("yyyy", NextBirthDate, MaxDateValue) = 0 Then ' No later birthday can be calculated. Else NextBirthDate = DateAdd("yyyy", Years + 1, BirthDate) End If End If End If End If DateNextBirthday = NextBirthDate End Function
So, to run a query in Access, you would do something like this:
Select Id, FirstName, LastName, DateNextBirthday([DOB]) As NextBirthday From tblYourPersonTable Order By DateNextBirthday([DOB])
Please note that this will return the correct birthday also for leaplings (those born on February 29th) for any year.
Hi,
Try Below Query
select FirstName,LastName,DateofBirth from TableName A where Month(DateofBirth)>=Month(GETDATE()) and Day(DateofBirth)>Day(GETDATE()) order by DateofBirth
这篇关于如何从数据库中获取即将到来的bithdays的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文