我试图让那些完成了5,10,15,20等年的员工。 我想每季度检索一次数据 所以我在where子句中添加了switch语句
declare @CurrentDate date
set @CurrentDate=GETDATE() ;
select EmployeeID,FirstName+''+isnull(MiddleName,'')+''+LastName as EmployeeName,JoiningDate,DATEDIFF(year, isnull(JoiningDate,''),getdate()) Experience
from HRIS.dbo.EmpMaster
where
ActiveFlag=1 and
CASE
WHEN DATEPART(quarter, getdate()) =1 and month(JoiningDate) BETWEEN 1 and 3 then 1
WHEN DATEPART(quarter, getdate()) =2 and month(JoiningDate) BETWEEN 4 and 6 then 1
WHEN DATEPART(quarter, getdate()) =3 and month(JoiningDate) BETWEEN 7 and 9 then 1
WHEN DATEPART(quarter, getdate()) =4 and month(JoiningDate) BETWEEN 10 and 12 then 1
else 0
END
and DATEDIFF(year, isnull(JoiningDate,''),@CurrentDate)%5=0 and Year(JoiningDate)!=2019
我遇到如下错误
Error: An expression of non-boolean type specified in a context where a condition is expected, near 'and'.
我理解该错误是因为where子句中的情况未返回布尔值。
如何编写开关盒以使其返回布尔值
提前谢谢!
答案 0 :(得分:0)
嗯。 。 。为什么不简化呢?
WHERE ActiveFlag = 1 AND
DATEPART(quarter, getdate()) = DATEPART(quarter, JoiningDate)
这似乎简单得多。
将在= 1
之后添加END
来解决您的问题。但是我不推荐这种方法。