如何计算sql中出生日期的年龄 [英] How to calculate age from date of birth in sql

查看:1810
本文介绍了如何计算sql中出生日期的年龄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何计算sql中出生日期的年龄

我的表有DOB列,但是我需要Age如何在sql查询中计算它

How to calculate age from date of birth in sql
My table has column for DOB but I need Age how to calculate it in sql query

推荐答案

SELECT FLOOR(DATEDIFF(DAY, @BirthDate, @TargetDate) / 365.25)



参考:根据SQL中的出生日期计算年龄 [ ^ ]




Ref: Calculating age based on date of birth in SQL[^]
or

SELECT DATEDIFF(hour,@dob,GETDATE())/8766 AS AgeYearsIntTrunc



参考:如何计算年龄(在几年内)基于出生日期和getDate() [ ^ ]



参考:

sql age function [ ^ ]

计算出生日期的年龄 [ ^ ]

日期计算:计算年龄 [ ^ ]


Ref:How to calculate age (in years) based on Date of Birth and getDate()[^]

Refer:
sql age function[^]
calculate age on date of birth[^]
Date Calculations: calculate ages[^]


您好,



试试这个代码块



Hi ,

try this code block

  DECLARE @date datetime, @tmpdate datetime, @years int, @months int, @days int
SELECT @date = '2/29/04'

SELECT @tmpdate = @date

SELECT @years = DATEDIFF(yy, @tmpdate, GETDATE()) - CASE WHEN (MONTH(@date) > MONTH(GETDATE())) OR (MONTH(@date) = MONTH(GETDATE()) AND DAY(@date) > DAY(GETDATE())) THEN 1 ELSE 0 END
SELECT @tmpdate = DATEADD(yy, @years, @tmpdate)
SELECT @months = DATEDIFF(m, @tmpdate, GETDATE()) - CASE WHEN DAY(@date) > DAY(GETDATE()) THEN 1 ELSE 0 END
SELECT @tmpdate = DATEADD(m, @months, @tmpdate)
SELECT @days = DATEDIFF(d, @tmpdate, GETDATE())

SELECT @years, @months, @days


这是我的RAP报告对象,报告对象Pascal,如在ReportBuilder for Medisoft Report Professional中



This is my solution for RAP, Report Object Pascal, as in ReportBuilder for Medisoft Report Professional

procedure AgeOnCalc(var Value: Variant);

var currentyear, currentmonth, currentday : integer;
    patientyear, patientmonth, patientday : integer;

begin

  decodedate(CurrentDate, currentyear, currentmonth, currentday);
  decodedate(Patient['Date of Birth'], patientyear, patientmonth, patientday);
  if (patientmonth > currentmonth) or ((patientmonth = currentmonth) and (patientday > currentday)) then
    value := currentyear - patientyear - 1
  else
    value := currentyear - patientyear;

end;


这篇关于如何计算sql中出生日期的年龄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆