SQLTeam.com | Weblogs | Forums

Check the date using GETDATE function

Hi everyone,

Can anyone please help me using the GETDATE function code below?

I would like to check the date using the following:

If @Start_Date is less than 1-12 months then it should say 'PASSED W/IN 1 YEAR'

If @Start_Date is less than 24 months then it should say 'PASSED W/IN 2 YEARS'

Else if @Start_Date is over 24 the 'FAILED'

My Code:
DECLARE @START_DATE DATE

SET @START_DATE = '2021-05-01 00:00:00.000'
;
SELECT
CASE month(@START_DATE)
WHEN month(GETDATE()) - 12 THEN 'PASSED W/IN 1 YEAR'
WHEN month(GETDATE()) - 24 THEN 'PASSED W/IN 2 YEARS'
WHEN month(GETDATE()) - 25 THEN 'FAILED'
END AS PASS_FAIL

Many thanks in advance.

Jim

Please ignore.. I got it.
DECLARE
@START_DATE DATE,
@MTHS_DIFF INT
;

SET @START_DATE = '2021-05-01 00:00:00.000';
SET @MTHS_DIFF = DATEDIFF(m, @START_DATE, GETDATE());

SELECT
CASE
WHEN @MTHS_DIFF <= 12 THEN 'PASSED W/IN 1 YEAR'
WHEN @MTHS_DIFF <= 24 THEN 'PASSED W/IN 1 YEAR'
ELSE 'FAILED'
END AS PASS_FAIL
;