Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase «««56789»»

Fun(?) with DATETIME2 Expand / Collapse
Author
Message
Posted Tuesday, March 29, 2011 1:06 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, July 18, 2011 12:30 AM
Points: 2, Visits: 10
Plz Tell me new things in sql server 2008 , I want to know it and want to use this sql server for my next project.
Post #1085336
Posted Tuesday, March 29, 2011 7:40 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Today @ 4:08 PM
Points: 8,573, Visits: 9,081
vikram.gharge (3/29/2011)
SQL Server 2008 brought us some new date types including DATETIME2 (which appears better in every way than DATETIME). But what happens if you run the following script (it will only work in SQL Server 2008 or later)?

DECLARE @myDate DATETIME2SET @myDate = '1752-09-09'PRINT @myDate
By Richard Warr

Answer: It runs perfectly but you would expect it to fail.

Explanation: The DATETIME2 type removes the "pre-1753" restriction of DATETIME. But it is unable to cater for the fact that there was no such date as 9th September 1752. Were it perfect we'd see:

"Msg 241, Level 16, State 1, Line 3 Conversion failed when converting date and/or time from character string."
Because of the move to the Gregorian Calendar, Britain and its possessions (including the USA - this WAS 1752) lost 11 days in September so there was nothing between the 2nd and the 13th.
There's some fun to be had with 1751 as well but we'll leave that for another holiday.



This is just nonsense - the DATETIME2 datatype represents dates in the Gregorian calendar, not dates that have been used in the national calendar of Great Britain and its colonies; the date 9th September 1752 was not used in that national calendar, but there certainly is such a date in the Gregorian calendar. So DATETIME2 is already perfect, and preventing it from representing that date would render it imperfect, not improve it.


Tom
Post #1085520
Posted Tuesday, March 29, 2011 9:28 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Sunday, April 27, 2014 6:26 AM
Points: 1,521, Visits: 3,036
vikram.gharge (3/29/2011)
SQL Server 2008 brought us some new date types including DATETIME2 .............There's some fun to be had with 1751 as well but we'll leave that for another holiday.


Vikram,

What's your point in copy/pasting the QOD? Did you mean to comment and forgot?
Post #1085640
Posted Friday, July 15, 2011 6:23 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Friday, December 13, 2013 7:56 AM
Points: 939, Visits: 754
Poor question; how do you know what my expectations were?


Post #1142438
Posted Wednesday, August 10, 2011 5:40 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Thursday, July 3, 2014 2:45 AM
Points: 2,531, Visits: 536
I expected it to work and it did ( as it should in most countries except in brittain )

/Håkan Winther
MCITP:Database Developer 2008
MCTS: SQL Server 2008, Implementation and Maintenance
Post #1157532
Posted Wednesday, August 10, 2011 9:42 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Today @ 4:08 PM
Points: 8,573, Visits: 9,081
hakan.winther (8/10/2011)
I expected it to work and it did ( as it should in most countries except in brittain )

Not in most countries except Britain (note single t), in all countries including Britain. Datetime2 uses the Gregorian calendar, not any other calendar, so it is not a locale dependent type.


Tom
Post #1157758
Posted Wednesday, August 10, 2011 10:06 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 10:12 AM
Points: 4,394, Visits: 3,402
Tom.Thomson (8/10/2011)
. . .
Not in most countries except Britain (note single t), in all countries including Britain. Datetime2 uses the Gregorian calendar, not any other calendar, so it is not a locale dependent type.

Hmm... maybe it is not quite Gregorian. If you run this, --

DECLARE @type2 DATETIME2(7) = '1582-10-10';
PRINT DATEPART( dw, @type2 );

you will get 1, i.e., Monday, but that is wrong because there was no October 10 in 1582.
Post #1157790
Posted Wednesday, August 10, 2011 4:08 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Today @ 4:08 PM
Points: 8,573, Visits: 9,081
Revenant (8/10/2011)
Tom.Thomson (8/10/2011)
. . .
Not in most countries except Britain (note single t), in all countries including Britain. Datetime2 uses the Gregorian calendar, not any other calendar, so it is not a locale dependent type.

Hmm... maybe it is not quite Gregorian. If you run this, --

DECLARE @type2 DATETIME2(7) = '1582-10-10';
PRINT DATEPART( dw, @type2 );

you will get 1, i.e., Monday, but that is wrong because there was no October 10 in 1582.


Actually if you have Monday as day 1 you will get 7, because that date is a Sunday in the Gregorian calendar. I suspect you have the US default (datefirst = 7, so dw=1 on Sunday), since you presumably got 1.

I guess you and I mean different things by "The Gregorian Calendar"; if "Gregorian Calendar" means the national calendar of the United Kingdom of Poland and Lithuania, or some of the Italian states, or Spain, or Portugal that date didn't exist (in the national calendars of everywhere else in the Western world it did). If you mean the calendar using the 365.2525 day year in common use throughout the western world, in which today is 10 August 2011, extended backwards and forwards according to its regular rules without limit (which is the most common meaning, I believe) 10 Oct 1582 certainly does exist in that calendar.

Using calendars of particular nations can lead to strange results. For example in England the months of January and February did not legally exist in 1751, but they did exist in Scotland (Scottish Jan 1751 was English Jan 1750; almost everyone in England already used the Scottish dating for everything except legal documents and taxation; it was just the legal system in England that was out of step). Scotland had begun the year on 1 Jan since 1600, while up to and including 1751 England began the year on 25th March. That's where we get our 6 April to 5 April taxation year from - it was 5 April to 4 April from 1752 to 1799, then from 6 April to 5 April from 1800 to 1899 in order to retain the same Julian date; but it wasn't adjusted in 1900, which was the next year when an adjustment would have been needed to maintain the Julian connection (whether because the 1751 act was interpreted differently or because the powers that be had forgotten that that act had been interpreted in 1800 as demanding such an adjustment).


Tom
Post #1158120
Posted Wednesday, August 10, 2011 7:28 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Sunday, April 27, 2014 6:26 AM
Points: 1,521, Visits: 3,036
That's a great post, Tom, showing examples of the variety in calendar usage. It's truly fascinating in a way that appeals to my inner "geek". It also provides a great sample of reasons to consider DATETIME2 simply a data type and nothing more. It holds values of dates following certain construction rules (number of days in a month, which is a leap year....), but does not attempt to codify the varieties of calendar adjustments made in different locales over the centuries. I think that's what you were saying too, in the last sentence of the first paragraph.

I think that's a fair thing for MS to have done. If I'm storing dates of historical events in the eighteenth century, then I ought to make any duration calculations aware of the Old Style/New Style break in 1752, or any other anomalies like the Scottish/English calendar difference.
Post #1158156
Posted Wednesday, August 10, 2011 9:58 PM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 10:12 AM
Points: 4,394, Visits: 3,402
I logged that October 5-14, 1582, unaccounted-for gap as a bug against Windows. (SQL Server gets the weekday via a WinAPI call.)

I was told that this will be “soon” corrected for the cultures (languages) that were on the Roman calendar at that time. Win guys are not exactly sure what to do about countries that converted later; most notably Russian Orthodoxy, which converted only after the October Revolution of 1917. (What do you do if the date and time are marked as GMT but the culture is one of the Eastern Orthodoxies? Any suggestions for precedence of calculation?)

This goes all the way back to The Bard and his oft-quoted “time is out of joint,” which alluded to the fact that when he wrote Hamlet, the English calendar was out of sync with the Roman one.

I guess this means that someone will have to think through comparisons of dates on servers that are set to different cultures. (The mentioned October Revolution, which was logged on October 25, 1917, is now celebrated on November 7.)

Never a dull moment.
Post #1158182
« Prev Topic | Next Topic »

Add to briefcase «««56789»»

Permissions Expand / Collapse