views:

517

answers:

3

Hi,

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  A: 

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

Mr. Smith
sorry that was a typo, the code is on a system with no internet access. it was set to = not -now fixed above.
Audioillity
+2  A: 

Why not compare the dates directly?

CASE WHEN DATEDIFF(day, FieldDate, GETDATE()) = 0 THEN 1 ELSE 0 END AS tmpDate
LukeH
I was thinking of doing that, but was unsure how correctly it would pick up the date. However on a quick look it appears to work. I was looking for advice before on getting the date back without time, and the ways were pointing to my first method above.
Audioillity
Using `DATEDIFF` as above will be fine for *comparing* two dates.
LukeH
If you actually wanted to return just the date element as a `DATETIME` rather than a string then you can use `DATEADD(day, 0, DATEDIFF(day, 0, FieldDate))` etc which will have the time element set to `00:00:00.000`.
LukeH
+2  A: 

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
edosoft