The last Sunday is always in the last seven days of the month.
Here is a function that returns -1 if the wanted date is before the last sunday of the month, returns 0 if the wanted date is the last sunday of the month and returns 1 if the wanted date is after the last sunday of the month.
N 56°04'39.26"
E 12°55'05.63"
Here is a function that returns -1 if the wanted date is before the last sunday of the month, returns 0 if the wanted date is the last sunday of the month and returns 1 if the wanted date is after the last sunday of the month.
ALTER FUNCTION dbo.fnIsDayAfterLastSunday
(
@Date DATE
)
RETURNS TABLE
AS
RETURN (
WITH cteCalendar(theDate)
AS (
SELECT DISTINCT DATEADD(DAY, f.Delta, d.LastDay)
FROM (
VALUES (DATEADD(MONTH, DATEDIFF(MONTH, '18991231', @Date), '18991231'))
) AS d(LastDay)
CROSS APPLY (
VALUES (0),
(-1),
(-2),
(-3),
(-4),
(-5),
(-6)
) AS f(Delta)
)
SELECT SIGN(DATEDIFF(DAY, theDate, @Date)) AS Position
FROM cteCalendar
WHERE DATEDIFF(DAY, '18991231', theDate) % 7 = 0
)
N 56°04'39.26"
E 12°55'05.63"
No comments:
Post a Comment