- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Admin
Lousy Smarch calculations.
Admin
Depending on the data set and how its used this may not be wrong Sometimes you want consistent time scales for metrics. IE months to complete should be calculated as datediff(d, start, end) / 30. Yes some months are not thirty days long but you might want to treat them like that anyway so you can compare data points.
Admin
Cumulative round off error.
Start with 1 year equals 356 days (a dyslexic input error). Dividing by 2 gives six months = 178 days. Add 178 to get to 12 months = 356 days. Add another 178 to get to 18 months = 534 days. Add another 178 to get to 24 months = 712 days.
A carpenter would have the same type of error if he cut a short measuring stick and then used that stick to measure a room. Especially if he cut the stick to the wrong length in the first place.
Admin
After puzzling over this a bit, I finally came to wonder if this code was generated by something. All of the day counts are precisely related to the 356 "days per year".
So maybe it was generated from a typo'd 365?
Admin
Admin
Odd that nobody's pointed out the obvious redundancy in the range checks, could just be
WHEN DATEDIFF(day, f.FileDate, @ReportDate) > 1825 THEN '5y+' WHEN DATEDIFF(day, f.FileDate, @ReportDate) > 1460 THEN '4-5y' WHEN DATEDIFF(day, f.FileDate, @ReportDate) > 730 THEN '2-3y' etc. ELSE '0-6m'
And yes, SQL server evaluates the conditions in the order specified.
Admin
Looks like you have a bug in both cases.
Admin
PHB logic: "it doesn't need to be right, it just needs to be consistent"
maintenance dev logic: "I'll just do what I'm told"
proper logic: "USE ALL THE UNIT TESTS!!!!"
Admin
I'm gonna assume I'm the first dumb person who doesn't understand why checking for more than 182 seconds for 6 minutes is any more correct than checking for more than 178 seconds.....
Admin
never mind....
Admin
Admin
Oh FFS....like something like the age of a file really needs to be all that accurate....
Admin
TRWTF is the way the Western World tracks time and date....amirite?
Admin
Which is kind of debatable. You are saying that when reports are printed at different times, two children of identical age should be classified differently depending on the vagaries of the calendar.
Admin
That's not the Tsar's daughter, that's the woman who used to dance with Hawkwind.
Admin
Faced with the need to have time periods that were rigidly divisible into certain buckets for time-series graphs, we used 180 days for "six months", to divide into nominal 30 day months, but 52*7 or 364 day "years" that graphed as 52 weeks.
The results of fractions or truncation produced far worse wrong answers than these wrong answers.
Admin
Admin
Certainly could use a common table expression to memoize it whether in T-SQL or view. Could also use multiple CTEs to get the "x-y months" calculations. Personally find them quite easy to read. Of course, SQL Server version matters.
But, yes, the datediff call cost is trivial here.
And I concur with others that the SQL Server DATEDIFF results are counter intuitive. No longer work on that database but IIRC, I would usually do DATEDIFF on year, month and date when I found it necessary to actually use DATEDIFF.
Admin
Yes...the entire point of this website.
Clue: We're here to snigger.
Admin
Was thinking about this last night, and there is a better way (given how bad DATEDIFF is at handling edge cases):
DATEADD.
It might not work for every date problem, but in this situation it fits perfectly, and it should cope with the edge cases automatically. So instead of
or the agreeably worse
you can use
How many internets do I win? ;)
Admin
Anastasia time is easy
A normal month is 30 days February is 28 days
28+5*30 = 178 = 6 month
1 year is the repeat of that
and so on.
Admin
So 0 + 24167 - 24167 -1 = (drum roll.........) MINUS ONE!
Admin
Admin
Admin
Anastasia used a Jewish calender, didn't she? A month defined by the moon is some 29.53 days, just a littel rounding error awy from here calculation. But some Jewish years have 13 months - why did she not take care of that?
Admin
Well, the code with the '365 days in a year' calculations was probably good enough for what they wanted. Really, sometimes you don't care about total accuracy, just want something reasonably good.
This was probably for internal stats reports, nothing more significant than that
Many people interpret '6 months' as being the same as 'half a year', which is pretty much what this gave you.
If you DO need accuracy, use date addition functions in your DB - eg DATEADD - to add months, rather than using 'date difference' functions. That way you don't get rounding errors.
Admin
Funny the way both reports are unlogical but it is only an issue because there are diffrences between both reports.
I think the bug was introduced the moment someone started talking about an age of 0-6 months. How many days can go into one month? A six month period can have a variation of 4 days, depending on the use of these reports this can be a serious deviation.
Mucht rather I would like to use weeks, days or maybe years (1/2 year), much less open for interpretation. Unless the six months period is really 6 months and an alternative method of naming half a year.
Admin
Admin
Care to elaborate? I see no particular problem with August...
Admin
Correction: if the previous one was not true. In this freaking language, "not true" is not the same as "false" because of three-value logic associated with nulls.
Admin
Correction noted. Although I've got a lot of time for SQL's treatment of logic: at least it's honest enough to admit when it doesn't know something ;)
Admin
In the southern hemisphere, February has 31 days, while August has 28, except for leap years, when it has 29. let's see how gullible you are :)
Admin
oh, I thought that "good developer" logic would be: "Unit tests??? I'm still waiting on QA for those!! those guys are useless!"
Admin
Admin
Yahweh to the danger zone!
Admin
Seems like it could be correct if you don't count holidays.
Admin
How do you even know this? Are you a wizard?
Admin
Admin
I think I know where she got the number from.
She is probably dyslexic, and put 356 instead of 365 as the days in a year. Then, she multiplied by the correct factors (0.5 for 6 months, 1.5 for 18 months, etc) and got the other numbers.
Admin
WUT? I live in the southern hemisphere, so I can guarantee you that it doesn't work that way. We have February with 28 days on non leap years and 29 on leap years. August is always 31 days. You are probably confused with the fact we have summer when you have winter and vice versa.
Admin
You should have quoted my post.
Admin
Admin
Now quote the correct one.
Admin
Admin
No, yours was. :)
Admin
Admin
Exactly. It was after.
Admin
356.. 365.. same thing right?
Admin