Is there any way to calculate age of a person based on DOB and quarter of the provided year sql server? [closed]

htrmnn0y  于 2023-05-16  发布在  SQL Server
关注(0)|答案(1)|浏览(107)

Closed. This question needs to be more focused . It is not currently accepting answers.

Want to improve this question? Update the question so it focuses on one problem only by editing this post .

Closed last year.
This post was edited and submitted for review last year and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question

We have DOB = '01/12/1997' and Quarter = '2' and Year ='2020' and I want the list of employee whose age is 50 in 2nd quarter of year '2020'. How can i get this in SQL server.

ID Name DOB 1 John 1982-01-09 00:00:00 2 Sally 1990-05-20 00:00:00 3 Adam 1997-12-01 00:00:00

cigdeys3

cigdeys31#

Using DATЕDD() and DATEPART() is an option:

SELECT DOB
FROM (VALUES
   (CONVERT(date, '19971207')),
   (CONVERT(date, '19700607')),
   (CONVERT(date, '19771207')),
   (CONVERT(date, '19971108'))
) d (DOB)
WHERE 
   DATEPART(year, DATEADD(year, 50, DOB)) = 2020 AND
   DATEPART(quarter, DATEADD(year, 50, DOB)) = 2

Result:

DOB
1970-06-07

相关问题