Hello! It is necessary to display the first and last name and the maximum age I don't have the age column, I will calculate max(age) like this:

 select max(age) from (select DATEDIFF (YY, BirthDate, GETDATE()) - CASE WHEN DATEADD(YY, DATEDIFF(YY, birthdate, GETDATE()), birthdate) > GETDATE() THEN 1 ELSE 0 END AS [age] from Employees) x 

How can I, besides the maximum age, deduce the name / surname of the owner of this age? ps When in the first request, I specify, in addition to max(age) , also firstname , lastname does not work

  • select lastname, firstname, DATEDIFF (YY, BirthDate, GETDATE ()) - CASE WHEN DATEADD (YY, DATEDIFF (YY, birthdate, GETDATE ()), birthdate)> GETDATE () THEN 1 ELSE 0 END AS [age] from Employees this way it turns out the output of all names and surnames with age, but not with the maximum ( - DoneBass
  • I mean, you need to get the oldest person out? And what is the format of BirthDate? - Daniel Protopopov
  • @DanielProtopopov exactly like this, datetime - DoneBass
  • Why then do the conversion in the number of years? As far as I know in MSSQL, you can do the order by date asc - Daniel Protopopov
  • @DanielProtopopov I am quite a novice and I don’t know much, as I understand. Order by date asc will give me all the dates as well. I need the first name, last name and age of only the oldest - DoneBass

1 answer 1

 SELECT TOP 1 firstname, lastname, DATEDIFF(BirthDate, YY, GETDATE()) AS age ORDER BY BirthDate ASC 
  • one
    Thank! brilliantly simple!) - DoneBass