开发者

Check if anniversary month is within a range of months

I'm trying to execute some different logic in a stored procedure when processing the anniversary month. Using a simple IF(anniversary = MONTH(x)) works fine when only one month is processed.

However, when multiple months are processed, the differing logic needs to be executed if the anniversary month is between the start month and the end month. IF(anniversary BETWEEN MONTH(x) AND MONTH(y)) will not work over the new year (i.e. September–January).

The anniversary field is currently generated as MONTH(contract_start), but this can be changed (it was previously using MONTHNAME() befor开发者_JAVA百科e, I have no idea why).

I was thinking that it might be a good idea for anniversary to become a boolean, to make the IF function more readable.


Okay, I worked this out. It was simple once I wrote down the algoritm in pseudocode I've created a function to store the logic instead, but it's fairly easy to turn it into an expression if needed.

BEGIN
IF min != max
THEN
    IF YEAR(min) != YEAR(max)
    THEN
        IF MONTH(born) BETWEEN MONTH(min) AND 12
        OR MONTH(born) BETWEEN 1 AND MONTH(max)
            THEN RETURN 1;
            ELSE RETURN 0;
            END IF;
        ELSEIF MONTH(born) BETWEEN MONTH(min) AND MONTH(max)
            THEN RETURN 1;
            ELSE RETURN 0;
        END IF;
    ELSEIF MONTH(born) = MONTH(min)
    THEN RETURN 1;
    ELSE RETURN 0;
END IF;
END
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜