/*
* Sql得到生日(闰月考虑)
* @birthday 时间
* @flag 1:返回2月28日,0:返回3月1日
* 调用:Select dbo.GetBirthday(birthday,0) From ofUser ou Where userId='48e541e2-7883-416a-b4ee-d78ddde4d6b8'
*/
Create Function GetBirthday
(
@birthday Datetime,
@flag Int
)
Returns Datetime
As
Begin
Declare @BirthdayRet Datetime,
@BirthdayThis Datetime,
@BirthdayNext Datetime,
@today Datetime,
@dateBetween Int
Set @today = Convert(Datetime, Convert(Varchar(10), Getdate(), 120))
Set @dateBetween = Datediff(YY, @birthday, Getdate())
Set @BirthdayThis = Dateadd(YY, @DateBetween, @birthday)
Set @BirthdayNext = Dateadd(YY, @DateBetween + 1, @birthday)
If @flag = 1 --2 月29 日的生日计算为2 月28 日
Begin
If @today > @BirthdayThis
Set @BirthdayRet = @BirthdayNext
Else
Set @BirthdayRet = @BirthdayThis
End
Else
--2 月29 日的生日计算为3 月1 日
Begin
If @today > @BirthdayThis
If Day(@birthday) = 29
And Day(@BirthdayNext) = 28
Set @BirthdayRet = @BirthdayNext + 1
Else
Set @BirthdayRet = @BirthdayNext
Else
If Day(@birthday) = 29
And Day(@BirthdayThis) = 28
Set @BirthdayRet = @BirthdayThis + 1
Else
Set @BirthdayRet = @BirthdayThis
End
Return @BirthdayRet
End
Go