- 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
what do you mean fix it by 2020? it was tested in 1992!
Admin
Woah woah woah. Syntax highlighting? Nice.
Admin
A small step in a case-clause, but a big leap for some years…
Admin
If that is T-SQL then all that is to do a broken version of EOMONTH() which has been available since SQL Server 2012.
For version prior to that it is SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0))
thanks to https://blog.sqlauthority.com for the latter.
Admin
Whenever you find yourself writing "CAST(CAST(", reconsider.
Admin
In the run up to year 2000 I swear I saw a letter in one of the computing weeklies basically saying "Everyone has got the leap year test for 2000 wrong, why don't we just redefine 2000 as NOT a leap year?". I fear he was serious....
Admin
Even worse, CAST(CAST((CASE
Admin
But, but... Feb DOES have 28 days EVERY year!!!
Admin
Even better, EVERY month has 28 days every year.
Admin
Maybe, just maybe, SQL (of any flavour) isn't the right place to do this sort of calculation.
Admin
Only halfwits got it wrong. People with no knowledge at all got it right, every 4th year is a leap year. Clever people got it right - there’s a 100 year and a 400 years exception. Knowing only the 100 year exception makes you a halfwit.
Admin
By the way, there will be very rare problems if the code runs just before midnight.
Admin
what time zone?
Admin
Reminds me of when I was supporting a DOS application in 1997. Someone moved ahead the system date to 2000 and sent in a bug report, that the app got the year 2000 wrong. For those of you that are a little fuzzy on the concept, Every 4th year is a leap year, except every 100'th, which isn't, but that's still a bit off, so the rule is say yes to every 400'th. Our app, being an old DOS app, was unlikely to even make it to the year 2000, by 2000 most everybody would be using Windows, and the next bad day would be in the year 2400! Definitely post-DOS times. We declined to fix the bug.
Admin
Not true, October 1582 didn’t have 28 days, though it did have October 28.
Admin
A sunday, right?
Admin
That's not universally true - it depends on when the switch was made from the Julian calendar to the Gregorian calendar. For instance, the UK and its colonies (including what would become the US) didn't make the switch until 1752, and the short month that year was September.
Admin
Admin
Well somebody seems to have been using that little poem: 30 days has September, April, June and November All the rest have 31 And February’s great with 28 And Leap Year’s February’s fine with 29
I guess they forgot that last line.
Admin
Q. what's the longest month of the year?
A. October. It's got an extra hour.
Admin
Actually it won't have problems - All GetDates within a SQL statement will return the same value.
Admin
Actually it won't have problems - All GetDates within a SQL statement will return the same value.
Admin
I 'like' how he's doing GETMONTH() -1 all over the place. Since he's just comparing them to month constants it'd be faster, easier, and clearer just to compare against normal 1-based month numbers. 11 should be November, not December.
Admin
..what about those 13-month calendars?
Admin
Except September 1752 in England...
Admin
Nobody's raised this in all the kerfuffle about the leap years - although maybe that's due to it being so obvious as to be unworthy of remark, or because I'm misinterpreting things. Shouldn't it be MONTH(GETDATE())+1, not MONTH(GETDATE())-1? You want to translate FROM a zero-indexed value returned by MONTH() TO a one-indexed month number for proper comparison. That's adding one, not subtracting. You'll sometimes get the right answer anyway because the long and short months tend to alternate, but I don't think you have until 2020 to fix this.
Admin
We should just switch to a 28 day, 13 month calendar, and just change the 13th month to be either 29 or 30 days for leap years.
Admin
In North America, the extra hour from the end of Daylight Saving Time is in November, not October, so the six of the seven 31-day months are all tied for the longest (with March being one hour shorter from the start of DST).
Admin
You can also use LAST_DAY in MySQL, postgres and many other databases to find the end of the month.
Admin
With all those repeated calls to GETDATE() I just can't wait to see the confusion when it rolls over the month end midway through evaluating the function.
Admin
can you help an idiot like my facebook account was been hacked ang the link was changed and i copy it and paste it on google and i saw this site how can i retrieve my account??
Admin
I mean, whoever wrote it did know about variables, but the thought of using one to store MONTH(GETDATE())-1 just didn't happen.
Admin
I mean, whoever wrote it did know about variables, but the thought of using one to store MONTH(GETDATE())-1 just didn't happen.
Addendum 2018-12-19 09:12: Do you think this forum software is sophisticated enough to detect double-submissions?
Admin
Last Christmas I gave you my code, but the very next leap year you gave it away. This year, to save me from tears I'll give it to DailyWTF.
Admin
I mean, whoever wrote these two comments did know about variables, but the thought of using one to store "I mean, whoever wrote it did know about variables, but the thought of using one to store MONTH(GETDATE())-1 just didn't happen." just didn't happen.
Admin
Although for the last day of the previous month this Stack Overflow answer has a nice solution:
https://stackoverflow.com/a/29474852
Admin
Although subtracting one day might be a bit of a safer bet, like:
SELECT DATEADD(DAY, -1, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0))
Reason being if you were to insert the value that subtracts one second into a SMALLDATETIME column (or set a variable of that type) it'd round it back up to the first day of the current month :s
Admin
I used to flag that sort of stuff during code reviews. It always frustrated me that the same people that would do something like
would, over and over, refuse to understand why calling a moving target like GETDATE() repeatedly was a bad idea.
Admin
Damn, that is pretty nice, though still liable to return the incorrect date for SMALLDATETIME during the last 30 seconds of the day (and other data types/resolutions during the last n fractional seconds of the day):
DECLARE @dt DATETIME = '20181218 23:59:30', @sdt SMALLDATETIME; SET @sdt = DATEADD(DAY, -DAY(@dt), @dt); SELECT @sdt;
Different datetime resolutions are great until they throw up a difficult-to-replicate bug. I guess that's more of a general problem with altering the precision of things on the fly though.
Admin
Says who? The pope?
Admin
"the UK and its colonies (including what would become the US) didn't make the switch until 1752, and the short month that year was September."
I wonder if landlords still charged a full month's rent.
Admin
"We should just switch to a 28 day, 13 month calendar, and just change the 13th month to be either 29 or 30 days for leap years."
Or say that a year is 364 days. Who cares if the calendar year matches the solar year? So seasons would start one day earlier each year. So what? The convenience of having a more systematic calendar would be well worth it.
Admin
Hmmm probably not, there are cultural traditions associated with certain dates and seasons, a shifting year wouldn't work.
Admin
Alternatively to maintain quarters, we can do 28 28 35 28 28 35 28 28 35 28 28 36/37. We keep 7 day weeks, 12 months, quarters. Ideally we reset the year, January 1 is always Sunday.
For time, we split earth day into 100000 seconds (comparable to current seconds). 20 hours, 50 minutes, 100 seconds.
Admin
But 28 is approximately correct for February 2020?!
Admin
No. Month in T-SQL returns a value 1 to 12. He's subtracting 1 to get the previous month.