469,609 Members | 1,657 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,609 developers. It's quick & easy.

Same day last year problem

Hey,

Today is Thursday 16-Feb-06. The same thursday last year is 17-Feb-05.
Because I am comparing daily sales between years, I need to know the
date of the same weekdayname as last year, so that I am comparing
Saturdays with Saturdays, Sundays with Sundays, etc

ie, 16-feb-06 goes to 17-feb-05
28-july-06 goes to 27-july-05 and so on

I have tried all sorts of dateadd('2004-01-01'............. etc, and I
am all out of ideas.

Any suggestions would be very much appreciated.

Thanks
Ryan

Feb 16 '06 #1
5 3981
rj********@hotmail.com wrote:
Hey,

Today is Thursday 16-Feb-06. The same thursday last year is 17-Feb-05.
Because I am comparing daily sales between years, I need to know the
date of the same weekdayname as last year, so that I am comparing
Saturdays with Saturdays, Sundays with Sundays, etc

ie, 16-feb-06 goes to 17-feb-05
28-july-06 goes to 27-july-05 and so on

I have tried all sorts of dateadd('2004-01-01'............. etc, and I
am all out of ideas.


What exactly do you mean by 'the same thursday'?

Is it the third thursday in february? If so, how do you handle the
fifth xxx of february in a leap year?

Is it the thursday of week number n? If so, when does week 1 start?
First day of january? Then weeks start on different days in different
years.
First saturday (or sunday or wednesday...) of january? Then is it week
0 before that? Or week 53 of the previous year?

Depending on your defiinition of 'same', the answers will be different.
DAYOFYEAR and DAYNAME('2006-01-01') might be helpful.
--
Eric Lafontaine

Feb 16 '06 #2
.... and WEEK might be even more useful, I should have read a bit
further.
--
Eric Lafontaine

Feb 16 '06 #3
Eric,

Thanks for your reply.

I am onto something with subtracting 52 weeks from date1. This works,
until I encounter a February 29th...

Any clues from here?

Feb 16 '06 #4
rj********@hotmail.com wrote:
I am onto something with subtracting 52 weeks from date1. This works,
until I encounter a February 29th...
I don't think so. 52 weeks are 364 days, so you will have cases where
your "same date" falls in a different month. Leap years should not add
any disturbance.
Same old question: has your "same date" to be in the "same" month?
Any clues from here?


Combination of DAYOFWEEK and WEEK, see
http://dev.mysql.com/doc/refman/5.0/...functions.html
At least, you will get something standard and consistent.

And just thinking of it: are your sales depending in any way on such
things as Easter or Carnival? Any comparison is then due to be
flawed...
--
Eric Lafontaine

Feb 16 '06 #5
>Today is Thursday 16-Feb-06. The same thursday last year is 17-Feb-05.
Because I am comparing daily sales between years, I need to know the
date of the same weekdayname as last year, so that I am comparing
Saturdays with Saturdays, Sundays with Sundays, etc
Decide what you want. Given that one year ago from your date isn't
the right weekday, what do you want:
(a) The first <Weekday> *BEFORE* 1 year ago
(b) The first <Weekday> *AFTER* 1 year ago
(c) (a) or (b), whichever is closest.
And what happens if the result isn't in the previous year? (e.g.
the comparison date for Jan 2, XXXX might be Dec 31, XXXX-2 , or
the comparison date for Dec 31, XXXX might be Jan 2, XXXX.)
ie, 16-feb-06 goes to 17-feb-05
28-july-06 goes to 27-july-05 and so on
Seems like maybe you want (c), but that's unclear. You might end
up with one date being used as a comparison more than once, and
another not at all.
I have tried all sorts of dateadd('2004-01-01'............. etc, and I
am all out of ideas.


You can often do manipulation with days of the week by taking the
weekday number of the date you've got, subtract it from the weekday
number of the date you want, possibly taking that value mod 7, and
adding or subtracting that number of days with dateadd().

Gordon L. Burditt
Feb 16 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by rjfjohnson | last post: by
reply views Thread by Solution2021 | last post: by
reply views Thread by devrayhaan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.