[英]Calculate the age in Microsoft SQL Server Management Studio
I'm doing this but the result give me all the names who they are 30 years and less than 30 years. 我正在执行此操作,但结果使我获得了30岁以下且不到30岁的所有姓名。
This is the database: 这是数据库:
I used this code - where is it wrong? 我使用了这段代码-哪里出错了?
SELECT
GETDATE () AS FirstName,
FirstName, LastName,
DATEADD(DD, 30, GETDATE()) AS [DateOfDonate- DateOfBirth],
DATEADD(DD, 1-1-2000, GETDATE()) AS [DonateOfDate]
FROM
Donor
I need to solve this question: find the names of donors who their age is above 30 years and have donated since 1/1/2000 我需要解决这个问题:找出自2000年1月1日以来年龄在30岁以上的捐赠者的姓名
SELECT
*
FROM
donor
WHERE
[date-of-birth] <= DATEADD(YEAR, -30, GETDATE()) -- Anyone born more than 30 years ago
AND [date-of-donate] >= '2000-01-01' -- Anyone that donated since 1st Jan 2000
Sorry for the previous typo . 对不起,以前的错字。 I calculated with actual fields and then replaced with yours resulting into the mess.
我用实际的字段进行计算,然后用您的字段替换成乱七八糟的东西。 Please use below
请在下面使用
cast(datediff(Year,Date-Of-Birth,getdate()) as nvarchar(100))
+ ' Years , '
+ cast(datediff(Month,Date-Of-Birth,getdate())%12 as nvarchar(100))
+ ' months and '
+ cast(datediff(day,Date-Of-Birth,getdate()) as nvarchar(100))
+' days '
select FirstName+" "+ LastName from Donor
where datediff(year,Date-of-birth,getdate()) >30 and Date-of-donate>
convert(date,'Date-of-donate',103)=convert(date,'Date-of-
donate',103)='1/1/2000'
This will work for you 这对你有用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.