Hi @Remo522
The CASE expression has two formats:
--Simple CASE expression:
CASE input_expression
WHEN when_expression THEN result_expression [ ...n ]
[ ELSE else_result_expression ]
END
--Searched CASE expression:
CASE
WHEN Boolean_expression THEN result_expression [ ...n ]
[ ELSE else_result_expression ]
END
For more details about CASE WHEN, please refer to this document:CASE (Transact-SQL)
The error in your code is : Boolean expression needs to go in your WHEN clause, not in your THEN (which returns the value)
In this issue,if you want
CASE WHEN GETDATE() = 'first of this month'
THEN 'TransactionDate' between 'first day of last month' and 'last day of last month'
ELSE 'TransactionDate' between 'first day of last month' and 'current date'
END
Then try this :
WHERE t.TransactionDate >= DATEADD(MONTH, DATEDIFF(MONTH, 0, DATEADD(DAY, -1, GETDATE())),0)
AND t.TransactionDate < CASE WHEN DATEPART(DAY, GETDATE()) = 1
THEN DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()),0)
ELSE GETDATE() END
Best regards,
LiHong
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.