How to calculate age in T-SQL with years, months, and days

后端 未结 24 1555
无人共我
无人共我 2020-11-22 05:42

What would be the best way to calculate someone\'s age in years, months, and days in T-SQL (SQL Server 2000)?

The datediff function doesn\'t handle year

24条回答
  •  名媛妹妹
    2020-11-22 06:04

    Here is SQL code that gives you the number of years, months, and days since the sysdate. Enter value for input_birth_date this format(dd_mon_yy). note: input same value(birth date) for years, months & days such as 01-mar-85

    select trunc((sysdate -to_date('&input_birth_date_dd_mon_yy'))/365) years,
    trunc(mod(( sysdate -to_date('&input_birth_date_dd_mon_yy'))/365,1)*12) months,
    trunc((mod((mod((sysdate -to_date('&input_birth_date_dd_mon_yy'))/365,1)*12),1)*30)+1) days 
     from dual
    

提交回复
热议问题