You could use DATEDIFF() to determine the number of days between your StartDate and JoiningDate and evaluate if that is less than or equal to 28 days.
We'll use the ABS() function around DATEDIFF(). That gives us the absolute(positive) value to then evaluate against 28 days.
DECLARE @test TABLE
(
[name] CHAR(1)
, [JoiningDate] DATE
, [StartDate] DATE
);
INSERT INTO @test (
[name]
, [JoiningDate]
, [StartDate]
)
VALUES ( 'A', '03/21/2017', '05/25/2020' )
, ( 'B', '01/13/2020', '01/29/2020' )
, ( 'C', '04/07/2016', '05/21/2020' )
, ( 'D', '02/18/2020', '02/29/2020' )
, ( 'E', '11/18/2020', '02/29/2020' );
SELECT *
FROM @test
WHERE [JoiningDate] >= DATEFROMPARTS(YEAR(DATEADD(YEAR, -1, GETDATE())), 1, 1)
AND ABS(DATEDIFF(DAY, [StartDate], [JoiningDate])) <= 28;
Giving you results of:
name JoiningDate StartDate
---- ----------- ----------
B 2020-01-13 2020-01-29
D 2020-02-18 2020-02-29
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…