I am working with a query which contains a CASE
statement in the WHERE
clause. But SQL Server 2008 is giving some errors while executing it.
Also I want to get the FIRST day of previous month.
Can anyone please help me with the correct query?
Here is case part of the query:
WHERE
FISCALCALENDARYEAR.STARTDATE =
CASE FORMAT(FISCALCALENDARYEAR.STARTDATE,'MM') BETWEEN 01 AND 10
THEN DATEADD(m, -1, DATEADD(mm, DATEDIFF(m, 0, FISCALCALENDARYEAR.STARTDATE), 0))
ELSE FISCALCALENDARYEAR.STARTDATE BETWEEN '2015/01/01' AND '2015/12/31'
END
The error :
Incorrect syntax near the keyword 'between'. or Incorrect syntax near the ' >='
And error in then of case
Incorrect syntax
Update : Now i have error in the else block :
WHERE FISCALCALENDARYEAR.STARTDATE = CASE WHEN FORMAT(FISCALCALENDARYEAR.STARTDATE,'MM') between 01 AND 10
THEN DATEADD( m,-1,DATEADD(mm, DATEDIFF(m,0,FISCALCALENDARYEAR.STARTDATE), 0)) --get first day on the last month
ELSE
DIMENSIONFOCUSBALANCE.ACCOUNTINGDATE == FORMAT(GETDATE() ,'dd/MM/yyyy')
AND FISCALCALENDARYEAR.STARTDATE == FORMAT(GETDATE() ,'dd/MM/yyyy')
END