<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://sqlblog.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>SSIS Junkie : datetime</title><link>http://sqlblog.com/blogs/jamie_thomson/archive/tags/datetime/default.aspx</link><description>Tags: datetime</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>Standard Point-in-time and time-interval representations</title><link>http://sqlblog.com/blogs/jamie_thomson/archive/2009/12/29/standard-point-in-time-and-time-interval-representations.aspx</link><pubDate>Mon, 28 Dec 2009 23:25:16 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:20289</guid><dc:creator>jamiet</dc:creator><slash:comments>6</slash:comments><comments>http://sqlblog.com/blogs/jamie_thomson/comments/20289.aspx</comments><wfw:commentRss>http://sqlblog.com/blogs/jamie_thomson/commentrss.aspx?PostID=20289</wfw:commentRss><wfw:comment>http://sqlblog.com/blogs/jamie_thomson/rsscomments.aspx?PostID=20289</wfw:comment><description>&lt;p&gt;One requirement in any database implementation that I have ever worked on is that the notion of a point-in-time has to be represented in some way. The tool that I use on a regular basis, SQL Server, provides numerous datatypes that aid in the representation of a point-in-time and I’m sure that most people reading this will be au fait with them. They are:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/bb677243.aspx" target="_blank"&gt;time&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/bb630352.aspx" target="_blank"&gt;date&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/ms182418.aspx" target="_blank"&gt;smalldatetime&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/ms187819.aspx" target="_blank"&gt;datetime&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/bb677335.aspx" target="_blank"&gt;datetime2&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/bb630289.aspx" target="_blank"&gt;datetimeoffset&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Read more at &lt;a href="http://msdn.microsoft.com/en-us/library/ms186724.aspx" target="_blank"&gt;Date and Time Data Types and Functions (Transact-SQL)&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://sqlblog.com/blogs/jamie_thomson/image_6FC4CDFD.png"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="date and time data types transact-sql" border="0" alt="date and time data types transact-sql" src="http://sqlblog.com/blogs/jamie_thomson/image_thumb_24357A79.png" width="743" height="235" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;I suspect (though stand to be corrected) that the accuracy with which people wish to record a point-in-time is usually to the nearest day or the nearest second however until very recently neither of these accuracies was specifically catered for with a dedicated datatype. Up until the introduction of the &lt;a href="http://msdn.microsoft.com/en-us/library/bb630352.aspx" target="_blank"&gt;date&lt;/a&gt; datatype in SQL Server 2008 &lt;a href="http://msdn.microsoft.com/en-us/library/ms187819.aspx" target="_blank"&gt;datetime&lt;/a&gt; was the datatype that most would use to record a point-in-time however the accuracy of datetime is neither day nor second, it is 0.00333 seconds; smalldatetime could be used but that also does not have accuracy to the day or second, it is to the minute.&lt;/p&gt;  &lt;p&gt;The point I’m trying to make is not that people are using the wrong datatypes to store their data, nor that everyone should just use the most accurate point-in-time datatype (that being &lt;a href="http://msdn.microsoft.com/en-us/library/bb677335.aspx" target="_blank"&gt;datetime2&lt;/a&gt; which is accurate to 100 nanoseconds). No, my point is that there are many different situations which have a need to store a point-in-time and SQL Server does not provide datatypes to cater for all of them. That is what prompted me to raise a Connect submission entitled &lt;a href="http://bit.ly/5MtIEC" target="_blank"&gt;[T-SQL] Month &amp;amp; Quarter datatype&lt;/a&gt; where I asked for datatypes that represent a Month or a Quarter. &lt;a href="http://sqlblogcasts.com/blogs/simons/" target="_blank"&gt;Simon Sabin&lt;/a&gt; commented that he also wanted datatypes to represent a time &lt;em&gt;interval&lt;/em&gt; (and &lt;a href="http://sqlblog.com/blogs/aaron_bertrand/default.aspx" target="_blank"&gt;Aaron Bertrand&lt;/a&gt; pointed out that a Connect submission &lt;a href="https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=432281" target="_blank"&gt;already exists&lt;/a&gt; for that) whilst &lt;a href="http://msmvps.com/blogs/robfarley/" target="_blank"&gt;Rob Farley&lt;/a&gt; ran with my idea and suggested that we should have week, hour, semester datatypes as well. There are some good justifications on the submission for why such datatypes are required so if I have failed to convince you here feel free to click through and take a read.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;The point of this blog post though is not to drum up support for my Connect submission (though that would be nice), instead it is to make reference to the reply I received from a gentleman on the SQL Server team going by the name ‘Jim’. Jim pointed me to &lt;a href="http://bit.ly/7lwaih" target="_blank"&gt;an article on Wikipedia&lt;/a&gt; which talks about the &lt;a href="http://bit.ly/8t5gw0" target="_blank"&gt;ISO 8601 standard&lt;/a&gt; that provides an agreed standard for the representation and exchange of date and time related data.&lt;/p&gt;  &lt;p&gt;I previously assumed that the only standard date/time formats were something like YYYYMMDD &amp;amp;&amp;#160; YYYYMMDDTHH24:MI:SS but not so, ISO 8601 provides representations for centuries, years, months, weeks, ordinal dates, hours, minutes, seconds, fractions of time units, timezones and time intervals (but not quarter, unfortunately). I won’t go into much detail about the standard as you can go and read about it for yourself (and I recommend the wikipedia article as essential reading for any data professional, particularly the &lt;a href="http://bit.ly/6tqYSp" target="_blank"&gt;General Principles&lt;/a&gt;) but for the benefit of both myself and others I have provided below a summary of all the point-in-time representations that the wikipedia article mentions; and fascinating it is too (to me anyway). Take the time to have a read, you may be enlightened:&lt;/p&gt;  &lt;table cellspacing="6" cellpadding="2"&gt;     &lt;tr&gt;       &lt;td&gt;&lt;strong&gt;Valid unambiguous representation&lt;/strong&gt;&lt;/td&gt;        &lt;td&gt;&lt;strong&gt;Description&lt;/strong&gt;&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td&gt;21&lt;/td&gt;        &lt;td&gt;21st Century&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td&gt;1981&lt;/td&gt;        &lt;td&gt;Year 1981&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td&gt;2004-05 (but not 200405)&lt;/td&gt;        &lt;td&gt;5th month of the year 2004&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td&gt;20090106          &lt;br /&gt;2009-01-06&lt;/td&gt;        &lt;td&gt;6th day of the 1st month of the year 2009&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td&gt;2006W34          &lt;br /&gt;2006-W34&lt;/td&gt;        &lt;td&gt;34th week of the year 2006&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td&gt;2006W343          &lt;br /&gt;2006-W34-3&lt;/td&gt;        &lt;td&gt;3rd day of the 34th week of the year 2006&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td&gt;1981095          &lt;br /&gt;1981-095&lt;/td&gt;        &lt;td&gt;95th day of the year 1981 (better known as 1981-04-05)&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td&gt;21&lt;/td&gt;        &lt;td&gt;21st hour of a day&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td&gt;2135          &lt;br /&gt;21:35&lt;/td&gt;        &lt;td&gt;35th minute of the 21st hour of a day&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td&gt;213507          &lt;br /&gt;21:35:07&lt;/td&gt;        &lt;td&gt;7th second of the 35th minute of the 21st hour of a day&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td&gt;0000          &lt;br /&gt;00:00           &lt;br /&gt;2400           &lt;br /&gt;24:00&lt;/td&gt;        &lt;td&gt;Midnight&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td&gt;1430.5          &lt;br /&gt;14:30.5&lt;/td&gt;        &lt;td&gt;14 hours, 30 minutes and one half minutes&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td&gt;143050.25          &lt;br /&gt;14:30:50.25&lt;/td&gt;        &lt;td&gt;14 hours, 30 minutes, 50 seconds and one quarter second&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td&gt;0930Z          &lt;br /&gt;09:30Z&lt;/td&gt;        &lt;td&gt;9 hours and 30 minutes coordinated universal time (aka UTC time aka Zulu time)&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td&gt;+0100          &lt;br /&gt;+01:00&lt;/td&gt;        &lt;td&gt;1 hour ahead of UTC&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td&gt;1830Z          &lt;br /&gt;22:30+04           &lt;br /&gt;1130-0700           &lt;br /&gt;15:00-03:30&lt;/td&gt;        &lt;td&gt;18 hours and 30 minutes UTC          &lt;br /&gt;          &lt;br /&gt;(the point being that these all represent the same point-in-time)&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td&gt;2007-04-05T14:30&lt;/td&gt;        &lt;td&gt;30th minute of the 14th hour of the 5th day of the 4th month of the year 2007          &lt;br /&gt;(“T” is the standard separator between date and time)&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td&gt;2007-04-05T24:00          &lt;br /&gt;2007-04-06T00:00&lt;/td&gt;        &lt;td&gt;Midnight inbetween the 5th &amp;amp; 6th days of the 4th month of the year 2007&lt;/td&gt;     &lt;/tr&gt;   &lt;/table&gt;  &lt;p&gt;Some things to note:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Some representations are overloaded (e.g. 21 can mean the 21st century or 21st hour of the day) so context can be a factor. &lt;/li&gt;    &lt;li&gt;The week notation is still prone to ambiguities; when does week 1 start? More pertinently, when does week 2 start? Apparently the ISO defines a standard for defining this but it isn’t adhered to as stringently as ISO 8601. &lt;/li&gt;    &lt;li&gt;The week notation can also be quite confusing, if the ISO directives are followed to the letter then “2009-W53-7” represents the 3rd day of the first month of 2010 (read the &lt;a href="http://bit.ly/7lwaih" target="_blank"&gt;wikipedia article&lt;/a&gt; to understand why). &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;The article also provides some standard representations of durations:&lt;/p&gt;  &lt;table cellspacing="6" cellpadding="2"&gt;     &lt;tr&gt;       &lt;td&gt;&lt;strong&gt;Valid unambiguous representation&lt;/strong&gt;&lt;/td&gt;        &lt;td&gt;&lt;strong&gt;Description&lt;/strong&gt;&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td&gt;P3Y&lt;/td&gt;        &lt;td&gt;A period of 3 years&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td&gt;P6M&lt;/td&gt;        &lt;td&gt;A period of 6 months&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td&gt;P4D&lt;/td&gt;        &lt;td&gt;A period of 4 days&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td&gt;P12H&lt;/td&gt;        &lt;td&gt;A period of 12 hours&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td&gt;PT6M&lt;/td&gt;        &lt;td&gt;A period of 6 minutes (note the T designation)&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td&gt;P5S&lt;/td&gt;        &lt;td&gt;A period of 5 seconds&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td&gt;P3Y6M4DT12H30M5S&lt;/td&gt;        &lt;td&gt;A period of three years, six months, four days, twelve hours, thirty minutes, and five seconds&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td&gt;P23DT23H&lt;/td&gt;        &lt;td&gt;A period of 23 days and 23 hours&lt;/td&gt;     &lt;/tr&gt;   &lt;/table&gt;  &lt;p&gt;and time intervals:&lt;/p&gt;  &lt;table cellspacing="6" cellpadding="2"&gt;     &lt;tr&gt;       &lt;td&gt;&lt;strong&gt;Valid unambiguous representation&lt;/strong&gt;&lt;/td&gt;        &lt;td&gt;&lt;strong&gt;Description&lt;/strong&gt;&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td&gt;2007-03-01T00:00:00Z/2008-05-11T15:30:00Z&lt;/td&gt;        &lt;td&gt;The interval between:          &lt;ul&gt;           &lt;li&gt;midnight (UTC) at the start of the first day of the 3rd month of the year 2007 &lt;/li&gt;            &lt;li&gt;the 30th minute of the 15th hour (UTC) of the 11th day of the 5th month of the year 2008 &lt;/li&gt;         &lt;/ul&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td&gt;2007-03-01T13:00:00Z/P1Y2M10DT2H30M&lt;/td&gt;        &lt;td&gt;The interval between:          &lt;ul&gt;           &lt;li&gt;the 13th hour (UTC) of the first day of the 3rd month of the year 2007 &lt;/li&gt;            &lt;li&gt;the point-in-time exactly 1 year, 2 months, 10 days, 2 hours and 30 minutes later &lt;/li&gt;         &lt;/ul&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/table&gt;  &lt;p&gt;One last interesting point of trivia, the second edition of ISO 8601 (published in 2000) allowed for 2-digit dates whereas the third (and at the time of writing, current) version (published in 2004) does not. Hmmm… did someone say Y2k? &lt;/p&gt;  &lt;hr /&gt;  &lt;p&gt;OK, time to wrap up; why am I writing this? Well, unambiguous representations of data are of paramount importance especially in the distributed heterogeneous XML/JSONified world of data exchange that we live in today so a good understanding of the standards that define those representations should be a pre-requisite to working in that arena. I hope this blog post has highlighted the fact that these standards exist and that you feel compelled to adhere to them in your future work. If you want to understand how damaging it can be to veer from the unambiguous formats then take a read of my recent blog post &lt;a href="http://sqlblog.com/blogs/jamie_thomson/archive/2009/12/08/unambiguous-date-formats-t-sql-tuesday-001.aspx" target="_blank"&gt;Unambiguous date formats&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;Oh, and if you want to vote for a month datatype in SQL Server, &lt;a href="http://bit.ly/5MtIEC" target="_blank"&gt;be my guest&lt;/a&gt; :)&lt;/p&gt;  &lt;p&gt;&lt;a href="http://bit.ly/83B80w" target="_blank"&gt;@Jamiet&lt;/a&gt;&lt;/p&gt;&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=20289" width="1" height="1"&gt;</description><category domain="http://sqlblog.com/blogs/jamie_thomson/archive/tags/date+representation/default.aspx">date representation</category><category domain="http://sqlblog.com/blogs/jamie_thomson/archive/tags/datetime/default.aspx">datetime</category><category domain="http://sqlblog.com/blogs/jamie_thomson/archive/tags/ISO+8601/default.aspx">ISO 8601</category><category domain="http://sqlblog.com/blogs/jamie_thomson/archive/tags/sql+server/default.aspx">sql server</category><category domain="http://sqlblog.com/blogs/jamie_thomson/archive/tags/t-sql/default.aspx">t-sql</category></item><item><title>Unambiguous date formats : T-SQL Tuesday #001</title><link>http://sqlblog.com/blogs/jamie_thomson/archive/2009/12/08/unambiguous-date-formats-t-sql-tuesday-001.aspx</link><pubDate>Tue, 08 Dec 2009 20:23:41 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:19629</guid><dc:creator>jamiet</dc:creator><slash:comments>9</slash:comments><comments>http://sqlblog.com/blogs/jamie_thomson/comments/19629.aspx</comments><wfw:commentRss>http://sqlblog.com/blogs/jamie_thomson/commentrss.aspx?PostID=19629</wfw:commentRss><wfw:comment>http://sqlblog.com/blogs/jamie_thomson/rsscomments.aspx?PostID=19629</wfw:comment><description>&lt;p&gt;One of the most commonly used data types in SQL Server is &lt;font face="Courier New"&gt;[datetime]&lt;/font&gt; which unfortunately has some vagaries around how values get casted. A typical method for defining a &lt;font face="Courier New"&gt;[datetime]&lt;/font&gt; literal is to write it as a character string and then cast it appropriately. The cast syntax looks something like this:&lt;/p&gt;  &lt;blockquote&gt;   &lt;table cellspacing="0" cellpadding="0" bgcolor="#eeeeee"&gt;       &lt;tr&gt;         &lt;td&gt;           &lt;pre style="padding-bottom:10px;padding-left:20px;padding-right:20px;font-family:consolas,lucida console,courier new,courier;font-size:12px;padding-top:10px;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;DECLARE &lt;/font&gt;&lt;font color="#434343"&gt;@dt &lt;/font&gt;&lt;font color="blue"&gt;NVARCHAR&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="black"&gt;19&lt;/font&gt;&lt;font color="gray"&gt;) &lt;/font&gt;&lt;font color="blue"&gt;= &lt;/font&gt;&lt;font color="red"&gt;'2009-12-08 18:00:00'&lt;/font&gt;&lt;font color="gray"&gt;;
&lt;br /&gt;&lt;/font&gt;&lt;font color="blue"&gt;SELECT &lt;/font&gt;&lt;font color="magenta"&gt;CAST&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="#434343"&gt;@dt &lt;/font&gt;&lt;font color="blue"&gt;AS &lt;/font&gt;&lt;font color="black"&gt;datetime&lt;/font&gt;&lt;font color="gray"&gt;);
&lt;br /&gt;&lt;/font&gt;&lt;/pre&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
    &lt;/table&gt;
&lt;/blockquote&gt;

&lt;p&gt;Unfortunately in SQL Server 2005 the result of the cast operation may be dependent on your current language setting. You can discover your current language setting by executing:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;table cellspacing="0" cellpadding="0" bgcolor="#eeeeee"&gt;
      &lt;tr&gt;
        &lt;td&gt;
          &lt;pre style="padding-bottom:10px;padding-left:20px;padding-right:20px;font-family:consolas,lucida console,courier new,courier;font-size:12px;padding-top:10px;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;SELECT &lt;/font&gt;&lt;font color="magenta"&gt;@@LANGUAGE&lt;/font&gt;&lt;/pre&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
    &lt;/table&gt;
&lt;/blockquote&gt;

&lt;p&gt;To demonstrate how your language setting can influence the results of a cast take a look at the following code:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;table cellspacing="0" cellpadding="0" bgcolor="#eeeeee"&gt;
      &lt;tr&gt;
        &lt;td&gt;
          &lt;pre style="padding-bottom:10px;padding-left:20px;padding-right:20px;font-family:consolas,lucida console,courier new,courier;font-size:12px;padding-top:10px;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;ALTER&amp;#160; DATABASE &lt;/font&gt;&lt;font color="black"&gt;tempdb
&lt;br /&gt;&lt;/font&gt;&lt;font color="blue"&gt;SET&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="black"&gt;COMPATIBILITY_LEVEL &lt;/font&gt;&lt;font color="blue"&gt;= &lt;/font&gt;&lt;font color="black"&gt;90 &lt;/font&gt;&lt;font color="gray"&gt;; &lt;/font&gt;&lt;font color="green"&gt;--Behave like SQL Server 2005
&lt;br /&gt;&lt;/font&gt;&lt;font color="blue"&gt;USE &lt;/font&gt;&lt;font color="black"&gt;tempdb
&lt;br /&gt;GO
&lt;br /&gt;&lt;/font&gt;&lt;font color="blue"&gt;DECLARE &lt;/font&gt;&lt;font color="#434343"&gt;@t &lt;/font&gt;&lt;font color="blue"&gt;TABLE &lt;/font&gt;&lt;font color="gray"&gt;(
&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="black"&gt;dateString&amp;#160; &lt;/font&gt;&lt;font color="blue"&gt;NVARCHAR&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="black"&gt;19&lt;/font&gt;&lt;font color="gray"&gt;)
&lt;br /&gt;);
&lt;br /&gt;&lt;/font&gt;&lt;font color="blue"&gt;INSERT &lt;/font&gt;&lt;font color="#434343"&gt;@t &lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="black"&gt;dateString&lt;/font&gt;&lt;font color="gray"&gt;)
&lt;br /&gt;&lt;/font&gt;&lt;font color="blue"&gt;VALUES &lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="red"&gt;'2009-12-08 18:00:00'&lt;/font&gt;&lt;font color="gray"&gt;) &lt;/font&gt;&lt;font color="green"&gt;--'yyyy-MM-dd hh24:mi:ss'
&lt;br /&gt;&lt;/font&gt;&lt;font color="gray"&gt;,&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; (&lt;/font&gt;&lt;font color="red"&gt;'2009-12-08T18:00:00'&lt;/font&gt;&lt;font color="gray"&gt;) &lt;/font&gt;&lt;font color="green"&gt;--'yyyy-MM-ddThh24:mi:ss'
&lt;br /&gt;&lt;/font&gt;&lt;font color="gray"&gt;,&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; (&lt;/font&gt;&lt;font color="red"&gt;'20091208 18:00:00'&lt;/font&gt;&lt;font color="gray"&gt;)&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="green"&gt;--'yyyyMMdd hh24:mi:ss'
&lt;br /&gt;
&lt;br /&gt;&lt;/font&gt;&lt;font color="blue"&gt;SET&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="black"&gt;LANGUAGE french&lt;/font&gt;&lt;font color="gray"&gt;;
&lt;br /&gt;&lt;/font&gt;&lt;font color="blue"&gt;SELECT &lt;/font&gt;&lt;font color="red"&gt;'french' &lt;/font&gt;&lt;font color="blue"&gt;AS &lt;/font&gt;&lt;font color="black"&gt;lang
&lt;br /&gt;&lt;/font&gt;&lt;font color="gray"&gt;,&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="magenta"&gt;DATENAME&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="magenta"&gt;MONTH&lt;/font&gt;&lt;font color="gray"&gt;,&lt;/font&gt;&lt;font color="black"&gt;q.[dt]&lt;/font&gt;&lt;font color="gray"&gt;) &lt;/font&gt;&lt;font color="blue"&gt;AS &lt;/font&gt;&lt;font color="black"&gt;mnth
&lt;br /&gt;&lt;/font&gt;&lt;font color="gray"&gt;,&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="black"&gt;q.[dt]
&lt;br /&gt;&lt;/font&gt;&lt;font color="blue"&gt;FROM&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="gray"&gt;(
&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="blue"&gt;SELECT&amp;#160; &lt;/font&gt;&lt;font color="magenta"&gt;CAST&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="black"&gt;dateString &lt;/font&gt;&lt;font color="blue"&gt;AS &lt;/font&gt;&lt;font color="black"&gt;DATETIME&lt;/font&gt;&lt;font color="gray"&gt;) &lt;/font&gt;&lt;font color="blue"&gt;AS &lt;/font&gt;&lt;font color="black"&gt;dt
&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="blue"&gt;FROM&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="#434343"&gt;@t
&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="gray"&gt;)&lt;/font&gt;&lt;font color="black"&gt;q&lt;/font&gt;&lt;font color="gray"&gt;;
&lt;br /&gt;&lt;/font&gt;&lt;font color="blue"&gt;SET&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="black"&gt;LANGUAGE us_english&lt;/font&gt;&lt;font color="gray"&gt;;
&lt;br /&gt;&lt;/font&gt;&lt;font color="blue"&gt;SELECT &lt;/font&gt;&lt;font color="red"&gt;'us_english' &lt;/font&gt;&lt;font color="blue"&gt;AS &lt;/font&gt;&lt;font color="black"&gt;lang
&lt;br /&gt;&lt;/font&gt;&lt;font color="gray"&gt;,&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="magenta"&gt;DATENAME&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="magenta"&gt;MONTH&lt;/font&gt;&lt;font color="gray"&gt;,&lt;/font&gt;&lt;font color="black"&gt;q.[dt]&lt;/font&gt;&lt;font color="gray"&gt;) &lt;/font&gt;&lt;font color="blue"&gt;AS &lt;/font&gt;&lt;font color="black"&gt;mnth
&lt;br /&gt;&lt;/font&gt;&lt;font color="gray"&gt;,&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="black"&gt;q.[dt]
&lt;br /&gt;&lt;/font&gt;&lt;font color="blue"&gt;FROM&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="gray"&gt;(
&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="blue"&gt;SELECT&amp;#160; &lt;/font&gt;&lt;font color="magenta"&gt;CAST&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="black"&gt;dateString &lt;/font&gt;&lt;font color="blue"&gt;AS &lt;/font&gt;&lt;font color="black"&gt;DATETIME&lt;/font&gt;&lt;font color="gray"&gt;) &lt;/font&gt;&lt;font color="blue"&gt;AS &lt;/font&gt;&lt;font color="black"&gt;dt
&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="blue"&gt;FROM&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="#434343"&gt;@t
&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="gray"&gt;)&lt;/font&gt;&lt;font color="black"&gt;q&lt;/font&gt;&lt;font color="gray"&gt;;&lt;/font&gt;&lt;/pre&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
    &lt;/table&gt;
&lt;/blockquote&gt;

&lt;p&gt;We are taking the value which can be described in words as “6pm on 8th December 2009”, defining it in three different ways, then seeing how the &lt;font color="#ff00ff" face="Courier New"&gt;@@LANGUAGE&lt;/font&gt; setting can affect the results. Here are those results:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;a href="http://sqlblog.com/blogs/jamie_thomson/image_1CC2DDFC.png"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="french language datetime" border="0" alt="french language datetime" src="http://sqlblog.com/blogs/jamie_thomson/image_thumb_3B2D1EE5.png" width="321" height="203" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Notice how the interpretation of the month can change depending on &lt;font color="#ff00ff" face="Courier New"&gt;@@LANGUAGE&lt;/font&gt;. If &lt;font face="Courier New"&gt;&lt;font color="#ff00ff"&gt;@@LANGUAGE&lt;/font&gt;=’french’ &lt;/font&gt;then the string '2009-12-08 18:00:00' is interpreted as 12&lt;sup&gt;th&lt;/sup&gt; August 2009 (‘août’ is French for August for those that don’t know) whereas if &lt;font face="Courier New"&gt;&lt;font color="#ff00ff"&gt;@@LANGUAGE&lt;/font&gt;=’us_english’ &lt;/font&gt;it is interpreted as 8&lt;sup&gt;th&lt;/sup&gt; December 2009.&lt;/p&gt;

&lt;p&gt;Clearly this is a problem because the results of our queries have a dependency on a server-level or connection-level setting and that is NOT a good thing. Hence I recommend that you only define &lt;font face="Courier New"&gt;[datetime]&lt;/font&gt; literals in one of the two unambiguous date formats:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;yyyy-MM-dd&lt;strong&gt;&lt;u&gt;T&lt;/u&gt;&lt;/strong&gt;HH24:mi:ss &lt;/li&gt;

  &lt;li&gt;yyyyMMdd HH24:mi:ss &lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;p&gt;That was going to be the end of this blog post but then I found out that this behaviour changed slightly in SQL Server 2008. Take the following code (see if you can figure out what the results will be before I tell you):&lt;/p&gt;

&lt;blockquote&gt;
  &lt;table cellspacing="0" cellpadding="0" bgcolor="#eeeeee"&gt;
      &lt;tr&gt;
        &lt;td&gt;
          &lt;pre style="padding-bottom:10px;padding-left:20px;padding-right:20px;font-family:consolas,lucida console,courier new,courier;font-size:12px;padding-top:10px;-moz-background-clip:border;-moz-background-origin:padding;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;ALTER&amp;#160; DATABASE &lt;/font&gt;&lt;font color="black"&gt;tempdb
&lt;br /&gt;&lt;/font&gt;&lt;font color="blue"&gt;SET&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="black"&gt;COMPATIBILITY_LEVEL &lt;/font&gt;&lt;font color="blue"&gt;= &lt;/font&gt;&lt;font color="black"&gt;100 &lt;/font&gt;&lt;font color="gray"&gt;; &lt;/font&gt;&lt;font color="green"&gt;--Behave like SQL Server 2008
&lt;br /&gt;&lt;/font&gt;&lt;font color="black"&gt;GO
&lt;br /&gt;&lt;/font&gt;&lt;font color="blue"&gt;USE &lt;/font&gt;&lt;font color="black"&gt;tempdb
&lt;br /&gt;GO
&lt;br /&gt;&lt;/font&gt;&lt;font color="blue"&gt;SET&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="black"&gt;LANGUAGE french&lt;/font&gt;&lt;font color="gray"&gt;;
&lt;br /&gt;&lt;/font&gt;&lt;font color="blue"&gt;DECLARE&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="#434343"&gt;@dt &lt;/font&gt;&lt;font color="magenta"&gt;NCHAR&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="black"&gt;10&lt;/font&gt;&lt;font color="gray"&gt;) &lt;/font&gt;&lt;font color="blue"&gt;= &lt;/font&gt;&lt;font color="red"&gt;'2009-12-08 18:00:00'&lt;/font&gt;&lt;font color="gray"&gt;; &lt;/font&gt;&lt;font color="green"&gt;--Ambiguous date format
&lt;br /&gt;&lt;/font&gt;&lt;font color="blue"&gt;SELECT &lt;/font&gt;&lt;font color="magenta"&gt;CAST&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="#434343"&gt;@dt &lt;/font&gt;&lt;font color="blue"&gt;AS &lt;/font&gt;&lt;font color="black"&gt;datetime&lt;/font&gt;&lt;font color="gray"&gt;) &lt;/font&gt;&lt;font color="blue"&gt;AS &lt;/font&gt;&lt;font color="black"&gt;[ExplicitCast]
&lt;br /&gt;&lt;/font&gt;&lt;font color="gray"&gt;,&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="magenta"&gt;DATENAME&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="magenta"&gt;MONTH&lt;/font&gt;&lt;font color="gray"&gt;,&lt;/font&gt;&lt;font color="#434343"&gt;@dt&lt;/font&gt;&lt;font color="gray"&gt;) &lt;/font&gt;&lt;font color="blue"&gt;AS &lt;/font&gt;&lt;font color="black"&gt;[MonthFromImplicitCast]
&lt;br /&gt;&lt;/font&gt;&lt;font color="gray"&gt;,&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="magenta"&gt;DATENAME&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="magenta"&gt;MONTH&lt;/font&gt;&lt;font color="gray"&gt;,&lt;/font&gt;&lt;font color="magenta"&gt;CAST&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="#434343"&gt;@dt &lt;/font&gt;&lt;font color="blue"&gt;AS &lt;/font&gt;&lt;font color="black"&gt;datetime&lt;/font&gt;&lt;font color="gray"&gt;)) &lt;/font&gt;&lt;font color="blue"&gt;AS &lt;/font&gt;&lt;font color="black"&gt;[MonthFromExplicitCast]&lt;/font&gt;&lt;font color="gray"&gt;;&lt;/font&gt;&lt;/pre&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
    &lt;/table&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here we are doing three different things with our nchar literal:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;explicitly cast it as a &lt;font face="Courier New"&gt;[datetime]&lt;/font&gt; &lt;/li&gt;

  &lt;li&gt;extract the month name from the char literal using the &lt;font color="#ff00ff"&gt;DATENAME&lt;/font&gt; function (which results in an under-the-covers implicit cast) &lt;/li&gt;

  &lt;li&gt;extract the month name from the char literal using the &lt;font color="#ff00ff"&gt;DATENAME&lt;/font&gt; function after it has been explicitly casted as a &lt;font face="Courier New"&gt;[datetime]&lt;/font&gt; &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Note that the compatibility level is set to SQL Server 2008 and &lt;font face="Courier New"&gt;&lt;font color="#ff00ff"&gt;@@LANGUAGE&lt;/font&gt;=’french’&lt;/font&gt;. Here are the results:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;a href="http://sqlblog.com/blogs/jamie_thomson/image_79B26C8B.png"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="image" border="0" alt="image" src="http://sqlblog.com/blogs/jamie_thomson/image_thumb_519FBD6C.png" width="431" height="87" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;(Were you correct?)&lt;/p&gt;

&lt;p&gt;Let’s take a look at what is happening here. The behaviour when we are explicitly casting as &lt;font face="Courier New"&gt;[datetime]&lt;/font&gt; hasn’t changed, our nchar literal is still getting interpreted as 12th August rather than 8th December when &lt;font face="Courier New"&gt;&lt;font face="Courier New"&gt;&lt;font color="#ff00ff"&gt;@@LANGUAGE&lt;/font&gt;&lt;/font&gt;=’french’&lt;/font&gt;. The &lt;font face="Courier New"&gt;[MonthFromExplicitCast]&lt;/font&gt; field is interesting though, it seems as though the implicit cast has resulted in the desired value of 8th December. Why is that?&lt;/p&gt;

&lt;p&gt;To get the answer we can turn to &lt;a href="http://msdn.microsoft.com/en-us/library/ms174395.aspx"&gt;BOL’s description&lt;/a&gt; of the &lt;font color="#ff00ff"&gt;DATENAME&lt;/font&gt; function syntax:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;a href="http://sqlblog.com/blogs/jamie_thomson/image_6C6BE378.png"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="image" border="0" alt="image" src="http://sqlblog.com/blogs/jamie_thomson/image_thumb_6B937D8E.png" width="244" height="85" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The implicit cast is not casting to &lt;font face="Courier New"&gt;[datetime]&lt;/font&gt; at all, it is actually casting to [date] which is a new datatype in SQL Server 2008. The new date-related datatypes in SQL Server 2008 (i.e. &lt;font face="Courier New"&gt;[date], [datetime2], [time], [datetimeoffset]&lt;/font&gt;) dis&lt;em&gt;regard&lt;/em&gt;&amp;#160;&lt;font face="Courier New"&gt;&lt;font color="#ff00ff"&gt;@@LANGUAGE&lt;/font&gt;&lt;/font&gt; and hence we get behaviour that is more predictable and, frankly, better.&lt;/p&gt;

&lt;p&gt;These new behaviours for SQL Server 2008 were unknown to me when I began this blog post so I have learnt something in the course of authoring it, I hope it has helped you too. No doubt someone somewhere is going to get nastily burnt by this at some point, make sure that it isn’t you by always using unambiguous date formats:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;yyyy-MM-ddTHH24:mi:ss &lt;/li&gt;

  &lt;li&gt;yyyyMMdd HH24:mi:ss &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;regardless of which version you are on!&lt;/p&gt;

&lt;p&gt;Don’t forget to check out other &lt;a href="http://sqlblog.com/tags/T-SQL+Tuesday/default.aspx"&gt;T-SQL Tuesday&lt;/a&gt; blog posts, see Adam Machanic’s blog post &lt;a href="http://sqlblog.com/blogs/adam_machanic/archive/2009/11/30/invitation-to-participate-in-t-sql-tuesday-001-date-time-tricks.aspx"&gt;Invitation to Participate in T-SQL Tuesday #001: Date/Time Tricks&lt;/a&gt; for details!&lt;/p&gt;

&lt;p&gt;&lt;a href="http://twitter.com/jamiet"&gt;@Jamiet&lt;/a&gt;&lt;/p&gt;&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=19629" width="1" height="1"&gt;</description><category domain="http://sqlblog.com/blogs/jamie_thomson/archive/tags/datetime/default.aspx">datetime</category><category domain="http://sqlblog.com/blogs/jamie_thomson/archive/tags/sql+server/default.aspx">sql server</category><category domain="http://sqlblog.com/blogs/jamie_thomson/archive/tags/t-sql/default.aspx">t-sql</category><category domain="http://sqlblog.com/blogs/jamie_thomson/archive/tags/T-SQL+Tuesday/default.aspx">T-SQL Tuesday</category><category domain="http://sqlblog.com/blogs/jamie_thomson/archive/tags/T-SQL+Tuesday+001/default.aspx">T-SQL Tuesday 001</category></item></channel></rss>