Question

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.

Was it helpful?

Solution 2

As far as I understand, you need this kind of usage;

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

OTHER TIPS

select @your_variable = case when DAY(GETDATE()) = 25
                             then MONTH(GETDATE())
                             else MONTH(GETDATE()) - 1
                        end 

You can use > and < operators with CASE. Is the following what you expected?

SELECT CASE WHEN DAY(GETDATE()) > 24
    THEN MONTH(GETDATE())
    ELSE MONTH(GETDATE()) + 1
END AS [a]
IF
 DAY(GETDATE()) <=25
 SELECT DATEADD(month,-1,CURRENT_TIMESTAMP)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top