How do I ensure that the following code only queries records that are submitted between March and May 2024?
If a record has been submitted between the above date range, display 1. Otherwise, display 0.
SELECT e.Name, e.email, e.emptitle, d.dateCreated,
CASE WHEN YEAR(d.dateSubmitted) < YEAR(getdate()) THEN 1 ELSE 0 END as previousYear,
--CASE WHEN YEAR(d.dateSubmitted) = YEAR(getdate()) THEN 1 ELSE 0 END as thisYear
CASE WHEN d.dateSubmitted BETWEEN DATEFROMPARTS(2024, 3, 1) AND DATEFROMPARTS(2024, 5 + 1, 1) THEN 1 ELSE 0 END as thisYear
FROM Employees e
INNER JOIN dateDetails d on e.employeeID = d.employeeID
WHERE e.employeeID = someID
The line of code I commented out works perfectly However, since we are directed to perform this check just one time between March and May 2024, we replaced the code we commented out with the line of code below it.
We are having problems getting consistent results
Any ideas?
I even tried the line below"
I have also tried this:
CASE WHEN d.dateSubmitted >= DATEFROMPARTS(2024, 3, 1) AND d.dateSubmitted < DATEFROMPARTS(2024,5 + 1, 1)
@ScottPletcher's code worked great but I am wondering if I can still do this< replace this:
' CASE WHEN YEAR(d.dateSubmitted) < YEAR(getdate()) THEN 1 ELSE 0 END as previousYear,
with this below and still get some result?
CASE WHEN d.dateSubmitted < '20240301' THEN 1 ELSE 0 END as previousYear,
In other words, since we are checking data entered from previous year up to March 1, 2024.
If dateSubmitted < from 03/01/2024 to prior year then previousYear = 1, otherwise, previousYear =1.
insert into @dateDetails Values ( 1,'2020-02-07','2024-03-31' )
insert into @dateDetails Values ( 2,'2022-10-15','2024-04-12' )
insert into @dateDetails Values ( 3,'2021-05-02','2024-05-10' )
SELECT
e.Name
, e.email
, e.emptitle
, d.dateCreated
, CASE WHEN cast(CONVERT(NCHAR(8), d.dateSubmitted,112) as int ) < 20240301 THEN 1 ELSE 0 END as previousYear
, CASE WHEN
cast(CONVERT(NCHAR(8), d.dateSubmitted,112) as int ) >= 20240301
AND
cast(CONVERT(NCHAR(8), d.dateSubmitted,112) as int ) < 20240501
THEN 1 ELSE 0 END as thisYear
FROM
@Employees e
INNER JOIN
@dateDetails d
on e.employeeID = d.employeeID