<?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>Search results matching tags 'T-SQL', 'datetime', and 'date representation'</title><link>http://sqlblog.com/search/SearchResults.aspx?o=DateDescending&amp;tag=T-SQL,datetime,date+representation&amp;orTags=0</link><description>Search results matching tags 'T-SQL', 'datetime', and 'date representation'</description><dc:language>en-US</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/28/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><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;</description></item></channel></rss>