Peter Larsson Blog

Patron Saint of Lost Yaks

Simple function to get beginning or end of month

CREATE FUNCTION dbo.fnIsOnMonthEdge
(
    @theDate DATETIME
)
RETURNS SMALLINT
AS
BEGIN
    RETURN CASE @theDate
                WHEN '99991231' THEN 1
                ELSE DATEDIFF(MONTH, @theDate, DATEADD(DAY, 1, @theDate))
            END +
            CASE @theDate
                WHEN '17530101' THEN -1
                ELSE DATEDIFF(MONTH, @theDate, DATEADD(DAY, -1, @theDate))
            END
END