I'm attempting to write a query that finds the user's work anniversary for the current month and considers a leap year as well (don't get an idea how to manage within the query)
Table "emp_detail":
emp_no | join_date |
---|---|
1 | 2002-06-10 |
2 | 2022-06-25 |
3 | 2020-02-29 |
4 | 2002-02-15 |
5 | 2011-02-01 |
So far I have tried the below query:
SELECT no,
join_date
CASE WHEN DATEADD(YY,DATEDIFF(yy,join_date,GETDATE()),join_date) < GETDATE()
THEN DATEDIFF(yy,join_date,GETDATE())
ELSE DATEDIFF(yy,join_date,GETDATE()) - 1
END AS 'anniversary'
FROM emp_detail
WHERE 'status' = 'active'
HAVING MONTH(join_date) = 06/07/08 -- ...so on
EDIT: Expected output:
For FEBRUARY month current year 2022
emp_no | join_date | anniversary_date |
---|---|---|
3 | 2020-02-29 | 2022-02-28 (Here, want get 29 Feb 2020 leap year record with non leap year 2022) |
4 | 2002-02-15 | 2022-02-15 |
5 | 2011-02-01 | 2022-02-01 |
Looking for a way to display employees with anniversary dates coming up at the start of the current month considering the leap year.
Am I going in the right direction? Any help would be great.