Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Requested Day vs Same Day Last Year - DateTime

Status
Not open for further replies.

Anddmx

Technical User
Apr 26, 2004
56
US
Hi,

I am haven problem trying to compare day from different years but I am trying to find the same day not date.

I have the following code so far but I there is something wrong not sure how to fix it but here is example.

Code

DECLARE @dteRequest AS DATETIME

Set @dteRequest = '06/14/2007'

select DATEADD(ww, DATEPART(ww, DATEADD(dd, -1, @dteRequest_Start)) - DATEPART(ww, @dteCompare_Start), @dteCompare_Start) as SameDayLastYear

OUTPUT = 2006-06-08 00:00:00.000

OUTPUT should be = 2006-06-15 00:00:00.000


Now if the year is 2007 day is off by week but I can change "DATEADD(dd, -1, @dteRequest_Start)" to "DATEADD(ww, 1, @dteRequest_Start)" and everything is fine but one problem, when the year changes to 2008 days are off again not sure how to fix this bug.

I just want to match the current day to last year day....

Sorry, hope I gave good example there, havent worked with dates to much.

Thanks
 
If you want the same day-of-year, how do you treat leap years?

select datediff(d,'01/01/2007',getdate())
select dateadd(yyyy,-1,getdate())
select datediff(d,'01/01/2006',dateadd(yyyy,-1,getdate()))

I know these need adjusted for dynamic operation, but I'm tired.

Phil Hegedusich
Senior Programmer/Analyst
IIMAK
-----------
Pity the insomniac dyslexic agnostic. He stays up all night, wondering if there really is a dog.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top