
October 8th, 2003, 08:36 AM
|
|
Junior Member
|
|
Join Date: Oct 2003
Location: Pensacola, FL
Posts: 1
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Please review, Calculating day of week of year formula
Hi,
The problem is to find the 3rd day of the 41st week of a year. This is used when comparing daily sales to sales one year ago. Comparing a Sunday to a Tuesday is like comparing apples to oranges, so we need to compare a Sunday to a Sunday.
The following formula seems to work. It's a little weird around the beginning and end of year. The calculated date may actaully be in the wrong year, but that should be ok.
Also does anyone know of an easier way to do this?
PHP Code:
declare @dt datetime
declare @lastyear datetime
set @dt = getdate()
--Get the week last year
set @lastyear = dateadd(yy,-1,@dt)
--Adjust the week
set @lastyear = dateadd(wk, (datepart(wk,@dt)-datepart(wk,@lastyear)), @lastyear)
--Get the day of the week
set @lastyear = dateadd(dd, datepart(dw,@dt)-datepart(dw,@lastyear) , @lastyear )
Thanks,
Jamison
Last edited by jamison : October 8th, 2003 at 08:53 AM.
|