{"id":88,"date":"2008-09-04T22:44:23","date_gmt":"2008-09-04T20:44:23","guid":{"rendered":"https:\/\/www.sqlinthewild.co.za\/?p=88"},"modified":"2016-05-24T11:24:22","modified_gmt":"2016-05-24T09:24:22","slug":"comparing-date-truncations","status":"publish","type":"post","link":"https:\/\/www.sqlinthewild.co.za\/index.php\/2008\/09\/04\/comparing-date-truncations\/","title":{"rendered":"Comparing date truncations"},"content":{"rendered":"<p>Removing the time from a datetime is a task that comes up again and again and it sometimes seems like everyone&#8217;s got their own favorite way of doing it. I though I&#8217;d take a run through some of the ways I&#8217;ve seen and see how the compare in terms of performance.<\/p>\n<p>The methods that I&#8217;ll be comparing are:<\/p>\n<ol>\n<li>Cast to varchar and back to datetime<\/li>\n<li>Cast to float, floor and cast back to datetime<\/li>\n<li>Use dateadd and datediff to get rid of the time<\/li>\n<li>Cast to date and back to datetime (SQL 2008 specific)<\/li>\n<\/ol>\n<p>I ran each one 4 times and averaged the results<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">CREATE TABLE #DateTest (\r\nID INT IDENTITY(1,1) PRIMARY KEY CLUSTERED,\r\nDateValue\u00a0 DATETIME\r\n)\r\n\r\nINSERT INTO #DateTest (DateValue)\r\nSELECT TOP 1000000\r\nDATEADD(mi,RAND(s1.number*5000 + s2.number)*5000,'2000\/01\/01' )\r\nFROM master..spt_values s1 CROSS JOIN master..spt_values s2\r\nWHERE s1.&#x5B;name] IS NULL AND s2.NAME IS null\r\nGO\r\n\r\nSET NOCOUNT ON\r\nGO\r\nSET STATISTICS TIME ON\r\nGO\r\n\r\nSELECT dateadd(dd, datediff(dd,0, DateValue),0)\r\nFROM #DateTest\r\nGO 4\r\n\r\nSELECT convert(datetime,convert(varchar(10),DateValue,101))\r\nFROM #DateTest\r\nGO 4\r\n\r\nSELECT CAST(FLOOR(CAST(DateValue AS FLOAT)) AS DATETIME)\r\nFROM #DateTest\r\nGO 4\r\n\r\nSELECT CAST(CAST(DateValue AS DATE) AS DATETIME)\r\nFROM #DateTest\r\nGO 4\r\n\r\nDROP TABLE #DateTest<\/pre>\n<p>And so, the winner of the date challenge:<\/p>\n<ol>\n<li>Dateadd and datediff &#8211; CPU time 348ms, execution time 8092 ms<\/li>\n<li>Cast to float and floor &#8211; CPU time 395ms, execution time 8270ms<\/li>\n<li>Cast to varchar &#8211; CPU time 1450ms, execution time 9280ms<\/li>\n<li>Cast to Date &#8211; CPU time 3063ms, execution time 7905ms<\/li>\n<\/ol>\n<p>I was not expecing the cast to Date to take so much processing time. I think I&#8217;m going to take a look at the new date types in more detail, see what they perform like.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Removing the time from a datetime is a task that comes up again and again and it sometimes seems like everyone&#8217;s got their own favorite way of doing it. I though I&#8217;d take a run through some of the ways&#8230; <a class=\"read-more-button\" href=\"https:\/\/www.sqlinthewild.co.za\/index.php\/2008\/09\/04\/comparing-date-truncations\/\">(Read more)<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[25,15],"tags":[],"class_list":["post-88","post","type-post","status-publish","format-standard","hentry","category-performance","category-sql-server"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p7h6n-1q","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/posts\/88","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/comments?post=88"}],"version-history":[{"count":4,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/posts\/88\/revisions"}],"predecessor-version":[{"id":1961,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/posts\/88\/revisions\/1961"}],"wp:attachment":[{"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/media?parent=88"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/categories?post=88"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/tags?post=88"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}