Friday, November 29, 2013

how to calculate givendate is before last sunday of the month or 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 (
SELECT DATEADD(MONTH, DATEDIFF(MONTH, '18991231', @Date), '18991231')
) AS d(LastDay)
CROSS APPLY (
SELECT 0 UNION ALL
SELECT -1 UNION ALL
SELECT -2 UNION ALL
SELECT -3 UNION ALL
SELECT -4 UNION ALL
SELECT -5 UNION ALL
SELECT -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