当前位置: 移动技术网 > IT编程>数据库>Mysql > 探讨如何计算age的sql函数

探讨如何计算age的sql函数

2017年12月12日  | 移动技术网IT编程  | 我要评论

经典qq心情,范达尔的研究,喀喇沁旗工资查询

复制代码 代码如下:

alter function [dbo].[get_fullage]
(
 @birthday datetime, @currentday datetime
)
returns int
as
begin
declare @age int
set @age = datediff(year, @birthday, @currentday)
if datediff(day, dateadd(year, @age, @birthday), @currentday) <= 0
 set @age = @age - 1
if datepart(month, @birthday) = 2 and datepart(day, @birthday) = 29 and datepart(month, @currentday) = 3
 and datepart(day, @currentday) = 1 and
 not (year(@currentday) % 4 = 0 and (year(@currentday) % 100 !=0 or year(@currentday) % 400 = 0))
 set @age = @age - 1
if @age < 0
 set @age = 0
  return @age
end

--sql根据出生日期计算age(不是很准确)
1.  select datediff(year,emp_birthday,getdate()) as '年龄' from  employeeunchangeinfo
2.  floor((datediff(day,u.emp_birthday,getdate()))/365

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网