CASE statement with GETDATE()

CSF90 picture CSF90 · Oct 3, 2013 · Viewed 9.6k times · Source

Wondering if you can help a little with the syntax here. Trying to set a variable as a month value dependent upon whether it is past the 25th day of the month.

If it is, then it uses the current month (e.g. the variable 'month' will be 10 if the date is 28th October, but will be 9 if it's the 24th October). So far I've got the following:

select a
case
    when (SELECT DAY(GETDATE()) >= 25
    then a = (SELECT MONTH(GETDATE()))
    else a = (SELECT MONTH(GETDATE()) - 1)
end

I understand you can't use less than or greater than signs, as case statements are only for evaluations (=)? Can anyone suggest another way of doing this?

Thanks.

Answer

juergen d picture juergen d · Oct 3, 2013
select @your_variable = case when DAY(GETDATE()) = 25
                             then MONTH(GETDATE())
                             else MONTH(GETDATE()) - 1
                        end