datediff函数计算俩个日期差
floor函数返回一个小于或等于当前值的最大整数
use StuManageDB
go
--定义变量
declare @birthday datetime,@days int ,@age int
--查询出生日期
Select @birthday=Birthday from Students where StudentId=100002
--计算出生天数
set @days =DATEDIFF(dayofyear,@birthday,getdate())
--计算年龄
set @age=floor(@days/365)
--输出信息
print '100002学员年龄:'+Convert(varchar(20),@age)
--直接查询
Select FLOOR(Datediff(dy,Birthday,Getdate())/365) as '年龄'
from Students where StudentId=100002