根據生日求年齡的SQL

kitesky發表於2006-11-28

精確到日。

[@more@]

-- 精確到日
declare @birthday datetime
declare @day datetime

set @birthday = '1975-10-18'
select @day = getdate()
select case
when datepart(dd, @day) >= datepart(dd, @birthday)
then datediff(mm, @birthday, @day)/12
else datediff(mm, @birthday,@day)/12 - 1
end

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/66009/viewspace-880415/,如需轉載,請註明出處,否則將追究法律責任。

相關文章