TSQL - Case Date Compare

余生长醉 提交于 2019-12-24 12:11:52

问题


I am trying to compare a date in SQL and create a new field to allow me to group the results by Todays date and other date(s). I am converting both dates to CHAR. However i am getting an expression error near = and ) - This is for an SSRS report.

There is already a fairly complex SQL statement and i am just trying to add this extra statement to help in grouping.

Here is an example.

Case WHEN Convert(Char(8), FieldDate, 103) = Convert(Char(8), GetDate(), 103) Then tmpDate = '1' ELSE tmpDate = '0' END

This results in an incorrect sntax near '=' This results in an incorrect sntax near ')'

Thanks


回答1:


Why not compare the dates directly?

CASE WHEN DATEDIFF(day, FieldDate, GETDATE()) = 0 THEN 1 ELSE 0 END AS tmpDate



回答2:


Move the field to the front:

select tmpDate = 
    CASE
    WHEN Convert(Char(8), FieldDate, 103) = Convert(Char(8), GetDate(), 103) Then  '1' 
    ELSE '0' END



回答3:


If your example is from your code (copy/paste), "Then tmpDate - '1'" won't work. You put a hyphen instead of an equal sign?



来源:https://stackoverflow.com/questions/1293154/tsql-case-date-compare

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!