<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://www2.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>What Happened Today? DATE and Date Ranges Over DATETIME</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2009/10/20/what-happened-today-date-and-date-ranges-over-datetime.aspx</link><description>A few days ago Aaron posted yet another fantastic entry in his Bad Habits series, this one discussing mishandling of date ranges in queries . This is a topic near and dear to me, having had to clean up a lot of poorly thought out code in the past few</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>re: What Happened Today? DATE and Date Ranges Over DATETIME</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2009/10/20/what-happened-today-date-and-date-ranges-over-datetime.aspx#18025</link><pubDate>Tue, 20 Oct 2009 19:02:57 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:18025</guid><dc:creator>AaronBertrand</dc:creator><description>&lt;p&gt;Yeah there was some follow-up in the comments on the optimizations for DATE. &amp;nbsp;I'm not crazy about using the CONVERT() on the LHS just because it is easy to change the code and forget that the CONVERT() is only used because it is an exception to the rule (I'm having deja vu that we have discussed habits here before). &amp;nbsp;&lt;/p&gt;
&lt;p&gt;I also suggest you stop using calculations like GETDATE()-1 as this will break if you do a massive search/replace for sysdatetime(). &amp;nbsp;:-)&lt;/p&gt;
</description></item><item><title>re: What Happened Today? DATE and Date Ranges Over DATETIME</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2009/10/20/what-happened-today-date-and-date-ranges-over-datetime.aspx#18026</link><pubDate>Tue, 20 Oct 2009 19:11:18 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:18026</guid><dc:creator>Dan Rolfe</dc:creator><description>&lt;p&gt;The rows from today.. a little extra writing but specific. Even the MS-Brain can figure it out logically&lt;/p&gt;
&lt;p&gt;&amp;lt;code&amp;gt;&lt;/p&gt;
&lt;p&gt;declare @date datetime&lt;/p&gt;
&lt;p&gt;set @date = '10/20/2009 23:59:59.999'&lt;/p&gt;
&lt;p&gt;SELECT COUNT(*) FROM [TABLE]&lt;/p&gt;
&lt;p&gt;WHERE (MONTH(Date_Field) = MONTH(@date) AND YEAR(Date_Field) = YEAR(@date) AND DAY(Date_Field) = DAY(@date))&lt;/p&gt;
&lt;p&gt;&amp;lt;/code&amp;gt;&lt;/p&gt;
</description></item><item><title>re: What Happened Today? DATE and Date Ranges Over DATETIME</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2009/10/20/what-happened-today-date-and-date-ranges-over-datetime.aspx#18029</link><pubDate>Tue, 20 Oct 2009 19:20:50 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:18029</guid><dc:creator>Adam Machanic</dc:creator><description>&lt;p&gt;Sorry, Dan, but that query will force a table scan to occur. Not a good idea.&lt;/p&gt;
</description></item><item><title>re: What Happened Today? DATE and Date Ranges Over DATETIME</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2009/10/20/what-happened-today-date-and-date-ranges-over-datetime.aspx#18030</link><pubDate>Tue, 20 Oct 2009 19:23:09 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:18030</guid><dc:creator>Adam Machanic</dc:creator><description>&lt;p&gt;Aaron: Yes, that's why I put the -1 inside the CONVERT :-) -- in this case it was just much easier to read than DATEADD. I know firsthand the pain of the overloads not being supported; I recently did a conversion of a 2005 DB to 2008, and along the way we changed all of the date/time columns in the database from [SMALLDATETIME with a constraint forcing midnight] to DATE. Suddenly lots and lots of code broke all over the place. Oops.&lt;/p&gt;
</description></item><item><title>re: What Happened Today? DATE and Date Ranges Over DATETIME</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2009/10/20/what-happened-today-date-and-date-ranges-over-datetime.aspx#18038</link><pubDate>Tue, 20 Oct 2009 20:35:26 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:18038</guid><dc:creator>AaronBertrand</dc:creator><description>&lt;p&gt;Placing the -1 inside the convert doesn't help. &amp;nbsp;This still yields a type clash error when you change GETDATE() to SYSDATETIME(), since it tries to subtract:&lt;/p&gt;
&lt;p&gt;CONVERT(DATE, SYSDATETIME()-1)&lt;/p&gt;
&lt;p&gt;I understand why you want to skip the DATEADD() syntax to make code less busy, but especially in the context of new DATE/TIME features I think it's dangerous to do so.&lt;/p&gt;
</description></item><item><title>re: What Happened Today? DATE and Date Ranges Over DATETIME</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2009/10/20/what-happened-today-date-and-date-ranges-over-datetime.aspx#18039</link><pubDate>Tue, 20 Oct 2009 20:35:57 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:18039</guid><dc:creator>AaronBertrand</dc:creator><description>&lt;p&gt;I think Dan missed the link to my date/range post. &amp;nbsp;:-)&lt;/p&gt;
</description></item><item><title>re: What Happened Today? DATE and Date Ranges Over DATETIME</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2009/10/20/what-happened-today-date-and-date-ranges-over-datetime.aspx#18048</link><pubDate>Tue, 20 Oct 2009 23:43:24 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:18048</guid><dc:creator>Sankar Reddy</dc:creator><description>&lt;p&gt;Just want to chime in and add this. In previous versions of SQL Server 2005, a lot of noise was made about datetime data type conversions and I know a lot of code was modified to use someting like below dateadd(dd,datediff(dd,0,getdate()),0). I stumbled across this post by CSS about a performance problem that the above code snippet has inherent problems with low estimates in SQL Server 2005/2008 leading to performance problems. Have any of you ran into this isse?&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://blogs.msdn.com/psssql/archive/2009/07/17/using-datediff-can-query-performance-problems-in-sql-2005-and-2008.aspx"&gt;http://blogs.msdn.com/psssql/archive/2009/07/17/using-datediff-can-query-performance-problems-in-sql-2005-and-2008.aspx&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>re: What Happened Today? DATE and Date Ranges Over DATETIME</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2009/10/20/what-happened-today-date-and-date-ranges-over-datetime.aspx#18049</link><pubDate>Wed, 21 Oct 2009 00:14:08 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:18049</guid><dc:creator>AaronBertrand</dc:creator><description>&lt;p&gt;I can see where this could cause an issue if your data distribution is heavily skewed, but in a normal distribution the estimate shouldn't affect performance by much at all. &amp;nbsp;I haven't seen the issue yet (though I now know to watch for it). &amp;nbsp;In any case, almost unilaterally when I am performing date range queries, the DATEADD/DATEDIFF calculations aren't actually part of the query; I apply them to the incoming parameter and/or create new parameters, depending on the other functionality in the procedure. &amp;nbsp;The query itself then uses the variable as opposed to a calculation that happens to use the variable. &amp;nbsp;Yes this can lead to parameter sniffing issues but certainly nothing due to the use of DATEADD/DATEDIFF vs. more primitive methods like converting to a string. &amp;nbsp;The big issue is when you essentially perform one or two calculations for every row, and this is certainly going to lead to more frequent, severe and predictable/preventable performance problems than an estimation problem.&lt;/p&gt;
</description></item><item><title>re: What Happened Today? DATE and Date Ranges Over DATETIME</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2009/10/20/what-happened-today-date-and-date-ranges-over-datetime.aspx#18054</link><pubDate>Wed, 21 Oct 2009 01:10:07 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:18054</guid><dc:creator>Adam Machanic</dc:creator><description>&lt;p&gt;Sankar: Agreed with Aaron; the potential issue seems like an edge case, not a normal situation. I have used the DATEADD/DATEDIFF techniques in dozens of projects--hundreds of queries--and have never seen any issues caused by estimates. It's definitely good to keep in mind but there are certainly bigger issues to dwell upon!&lt;/p&gt;
</description></item><item><title>re: What Happened Today? DATE and Date Ranges Over DATETIME</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2009/10/20/what-happened-today-date-and-date-ranges-over-datetime.aspx#18060</link><pubDate>Wed, 21 Oct 2009 06:30:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:18060</guid><dc:creator>Sankar Reddy</dc:creator><description>&lt;p&gt;Neither did I (and/or probably didn't even noticed) and was curious coming from PSS. Thanks for the feedback.&lt;/p&gt;
</description></item><item><title>re: What Happened Today? DATE and Date Ranges Over DATETIME</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2009/10/20/what-happened-today-date-and-date-ranges-over-datetime.aspx#18220</link><pubDate>Sun, 25 Oct 2009 13:49:20 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:18220</guid><dc:creator>Joe Celko</dc:creator><description>&lt;p&gt;Wouldn't it be faster to use the constant phi and the closed form?&lt;/p&gt;
&lt;p&gt;Fibonacci(@n INTEGER)&lt;/p&gt;
&lt;p&gt;AS &lt;/p&gt;
&lt;p&gt;RETURN&lt;/p&gt;
&lt;p&gt; EOUND (((POWER (1.6190339887, @n)- POWER (1.0 - 1.6190339887, @n))/ SQRT (5.0)), 0);&lt;/p&gt;
&lt;p&gt;untested. &amp;nbsp;Probably ought to cast phi as DOUBLE PRECISION&lt;/p&gt;
</description></item><item><title>re: What Happened Today? DATE and Date Ranges Over DATETIME</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2009/10/20/what-happened-today-date-and-date-ranges-over-datetime.aspx#18225</link><pubDate>Sun, 25 Oct 2009 14:33:42 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:18225</guid><dc:creator>Adam Machanic</dc:creator><description>&lt;p&gt;Joe: Wrong post. I think you meant to be here:&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://sqlblog.com/blogs/peter_larsson/archive/2009/10/18/simple-fibonacci-calculation.aspx"&gt;http://sqlblog.com/blogs/peter_larsson/archive/2009/10/18/simple-fibonacci-calculation.aspx&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Not sure what EOUND is, either :-)&lt;/p&gt;
</description></item><item><title>re: What Happened Today? DATE and Date Ranges Over DATETIME</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2009/10/20/what-happened-today-date-and-date-ranges-over-datetime.aspx#18578</link><pubDate>Fri, 06 Nov 2009 16:17:58 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:18578</guid><dc:creator>matt</dc:creator><description>&lt;p&gt;wow! &amp;nbsp;that's great! &amp;nbsp;thanks!&lt;/p&gt;
</description></item><item><title>Connect Digest : 2010-01-22</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2009/10/20/what-happened-today-date-and-date-ranges-over-datetime.aspx#21361</link><pubDate>Fri, 22 Jan 2010 20:46:35 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:21361</guid><dc:creator>Aaron Bertrand</dc:creator><description>&lt;p&gt;Give us easier to read execution plans Michelle Ufford ( @SQLFool ) recently asked for help pinpointing&lt;/p&gt;
</description></item></channel></rss>