I am trying to put a case statement into my Having Clause or Where Clause. Both come up with the same error "Error near '='" in the THEN statement. I'm not sure why this is not working. The idea of this statement is to look at the date and if it is the first of the month it will return last months data, if not it gives me this months data.
If someone could give me and idea as to why this doesn't work or another solution that would be great. Thanks
SELECT TOP 100 PERCENT database.Point_ time
FROM database
WHERE CASE WHEN datepart(dd, getdate())=1
THEN(MONTH(data base.Point_time ) = MONTH(getdate() )-1) AND
(YEAR(database. Point_time) = YEAR(getdate()) )
ELSE (MONTH(database .Point_time) = MONTH(getdate() )) AND
(YEAR(database. Point_time) = YEAR(getdate()) )
END
GROUP BY database.Point_ time
ORDER BY database.Point_ time.
If someone could give me and idea as to why this doesn't work or another solution that would be great. Thanks
SELECT TOP 100 PERCENT database.Point_ time
FROM database
WHERE CASE WHEN datepart(dd, getdate())=1
THEN(MONTH(data base.Point_time ) = MONTH(getdate() )-1) AND
(YEAR(database. Point_time) = YEAR(getdate()) )
ELSE (MONTH(database .Point_time) = MONTH(getdate() )) AND
(YEAR(database. Point_time) = YEAR(getdate()) )
END
GROUP BY database.Point_ time
ORDER BY database.Point_ time.
Comment