简体   繁体   English

在Microsoft SQL Server Management Studio中计算年龄

[英]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: 这是数据库:

  • Donor ( Donor-ID, First-name, Last-name, Date-of-birth, Sex, Date-of-donate) 捐赠者(捐赠者ID,名字,姓氏,出生日期,性别,捐赠日期)
  • Donor-phone (Donor-ID, Phone-number) 供体电话(供体ID,电话号码)
  • Clinic (Clinic-ID, Clinic-name, Clinic-Location) 诊所(诊所ID,诊所名称,诊所位置)
  • Blood (Blood-ID, Blood Type) 血液(血液ID,血型)
  • The donate day (Blood Status, Donate Date) 捐赠日(血液状态,捐赠日期)
  • Employee (Employee-ID, First-name, Last-name, Sex) 员工(员工编号,名字,姓氏,性别)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM