DECLARE@CurrentDate AS DATETIME
DECLARE@CurrentYear AS INTEGER
DECLARE@FirstDateInMarch AS DATETIME
DECLARE@FirstDateInNovember AS DATETIME
DECLARE@FirstDayInMarch AS VARCHAR(20)
DECLARE@FirstDayinNovember AS VARCHAR(20)
DECLARE@MarchDateAddValue AS INTEGER
DECLARE@NovDateAddValue AS INTEGER
DECLARE@DayLightBeingDate AS DATETIME -- 2nd Sunday of March
DECLARE@DayLightEndDate AS DATETIME -- 1st Sunday of Nov
SELECT@CurrentYear = DATEPART(YY,GETDATE())
--SELECT @CurrentYear = 2010
SELECT @FirstDateInMarch =CONVERT(DATETIME,'03/01/'+CONVERT(VARCHAR(4),@CurrentYear)),
@FirstDateInNovember = CONVERT(DATETIME,'11/01/'+CONVERT(VARCHAR(4),@CurrentYear))
SELECT@FirstDateInMarch,@FirstDateInNovember
SELECT
@FirstDayInMarch = DATENAME(w,@FirstDateInMarch),
@FirstDayInNovember = DATENAME(w,@FirstDateInNovember)
SELECT@FirstDayInMarch,@FirstDayInNovember
SELECT@MarchDateAddValue = CASE WHEN @FirstDayInMarch ='SUNDAY' THEN 14
WHEN @FirstDayInMarch ='MONDAY' THEN 13
WHEN @FirstDayInMarch ='TUESDAY' THEN 12
WHEN @FirstDayInMarch ='WEDNESDAY' THEN11
WHEN @FirstDayInMarch ='THURSDAY' THEN 10
WHEN @FirstDayInMarch ='FRIDAY' THEN 9
WHEN @FirstDayInMarch ='SATURDAY' THEN 8
END,
@NovDateAddValue = CASE
WHEN @FirstDayinNovember ='MONDAY' THEN 6
WHEN @FirstDayinNovember ='TUESDAY' THEN 5
WHEN @FirstDayinNovember ='WEDNESDAY' THEN 4
WHEN @FirstDayinNovember ='THURSDAY' THEN 3
WHEN @FirstDayinNovember ='FRIDAY' THEN 2
WHEN @FirstDayinNovember ='SATURDAY' THEN 1
WHEN @FirstDayinNovember ='SUNDAY' THEN 0
END
SELECT
@MarchDateAddValue,
@NovDateAddValue
SELECT
@DayLightBeingDate = DATEADD(dd,@MarchDateAddValue,@FirstDateInMarch),
@DayLightEndDate = DATEADD(dd,@NovDateAddValue,@FirstDateInNovember)
SELECT@DayLightBeingDate,@DayLightEndDate
SELECT DATENAME(dw,@DayLightBeingDate),DATENAME(dw,@DayLightEndDate)