[英]Count the amount of years from a certain date SQL
I want to count the amount of years from a the year the person is born to today. 我想计算从该人出生的那一年到今天的年数。 I have the components to use but dont know how to use them.
我有要使用的组件,但不知道如何使用它们。
SELECT COLUMN1, COLUMN2, DATEPART(YY,GETDATE()),
CONVERT(INT,+19)))LEFT(TABLE.COLUMN,2)
I want to use the +19 to show it before the birtyear. 我想在生日之前使用+19进行显示。 Example in the database the birthyear is showed as YY not YYYY.
数据库中的示例出生年份显示为YY而不是YYYY。 That is why I want to add the 19, so the SQL will count years from 19YY to 2013.
这就是为什么我要添加19,所以SQL将从19YY到2013年的年份。
Try this 尝试这个
SELECT id
, Name
, DATEDIFF(yy, CONVERT(DATETIME, DOB), GETDATE()) AS AGE
, DOB
FROM MyTable
declare @DOB date = '19680411'
select datediff(year, @DOB, getdate())- case when month(@DOB)*32 + day(@DOB) >
month(getdate()) * 32 + day(getdate()) then 1 else 0 end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.