{"id":618,"date":"2007-11-05T22:50:27","date_gmt":"2007-11-05T20:50:27","guid":{"rendered":"https:\/\/www.sqlinthewild.co.za\/index.php\/2007\/11\/05\/datetime-manipulation\/"},"modified":"2011-09-26T22:50:49","modified_gmt":"2011-09-26T20:50:49","slug":"datetime-manipulation","status":"publish","type":"post","link":"https:\/\/www.sqlinthewild.co.za\/index.php\/2007\/11\/05\/datetime-manipulation\/","title":{"rendered":"DateTime Manipulation"},"content":{"rendered":"<p>The date time data type and the date time functions within SQL are things that I see coming up time and time again in news groups and forums. Questions on how to get rid of the time, how to get the first day of the week, the last day of the month and so on. With the new Date and Time data types coming in SQL 2008, things will get easier, nut until then we have to do things the hard way.<\/p>\n<p>In systems I&#8217;ve worked on I&#8217;ve seen several implementations of functions to find the first and last day of a week, a month or a quarter. Some have worked well, some have worked and others, well, haven&#8217;t<\/p>\n<p><!--more-->So, here are some of the date functions that I use. The times are set so that these can be used in between statements and not pick up entries for the next interval.<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">DECLARE @TheDate DATETIME\r\nSET @TheDate = GetDate()\r\n\r\n\/* beginning of the day *\/\r\nselect dateadd(dd, datediff(dd,0, @TheDate),0)\r\n\/* end of the day*\/\r\nselect dateadd(ms,-3,dateadd(dd, datediff(dd,0, @TheDate)+1,0))\r\n\r\n\/* beginning of the week. Beware DATEFIRST settings.*\/\r\nselect dateadd(ww, datediff(ww,0, @TheDate),0)\r\n\/* end of the week *\/\r\nselect dateadd(ms,-3,dateadd(ww, datediff(ww,0, @TheDate)+1,0))\r\n\r\n\/* beginning of the month *\/\r\nselect dateadd(mm, datediff(mm,0, @TheDate),0)\r\n\/* end of the month. Does account for leap years*\/\r\nselect dateadd(ms,-3,dateadd(mm, datediff(mm,0, @TheDate)+1,0))\r\n\r\n\/* beginning of the quarter*\/\r\nselect dateadd(qq, datediff(qq,0, @TheDate),0)\r\n\/* end of the quarter *\/\r\nselect dateadd(ms,-3,dateadd(qq, datediff(qq,0, @TheDate)+1,0))\r\n\r\n\/* beginning of the year *\/\r\nselect dateadd(yy, datediff(yy,0, @TheDate),0)\r\n\/* end of the year *\/\r\nselect dateadd(ms,-3,dateadd(yy, datediff(yy,0, @TheDate)+1,0))<\/pre>\n<p>For ease, these can be created as UDFs.<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">-- The day, without the time\r\nCREATE FUNCTION dbo.DayStarts(@TheDate DATETIME)\r\nRETURNS DATETIME AS\r\nBEGIN\r\nRETURN dateadd(dd, datediff(dd,0, @TheDate),0)\r\nEND\r\nGO\r\n\r\n-- The day, 3 ms to midnight\r\nCREATE FUNCTION dbo.DayEnds(@TheDate DATETIME)\r\nRETURNS DATETIME AS\r\nBEGIN\r\nRETURN dateadd(ms,-3,dateadd(dd, datediff(dd,0, @TheDate)+1,0))\r\nEND\r\nGO\r\n\r\n-- First day of the week\r\nCREATE FUNCTION dbo.DateWeekStarts(@TheDate DATETIME)\r\nRETURNS DATETIME AS\r\nBEGIN\r\nRETURN dateadd(ww, datediff(ww,0, @TheDate),0)\r\nEND\r\nGO\r\n\r\n-- Last day of the week, 3 ms to midnight\r\nCREATE FUNCTION dbo.DateWeekEnds(@TheDate DATETIME)\r\nRETURNS DATETIME AS\r\nBEGIN\r\nRETURN dateadd(ms,-3,dateadd(ww, datediff(ww,0, @TheDate)+1,0))\r\nEND\r\nGO\r\n\r\n-- First day of the month\r\nCREATE FUNCTION dbo.DateMonthStarts(@TheDate DATETIME)\r\nRETURNS DATETIME AS\r\nBEGIN\r\nRETURN dateadd(mm, datediff(mm,0, @TheDate),0)\r\nEND\r\nGO\r\n\r\n-- Last day of the month, 3 ms to midnight\r\nCREATE FUNCTION dbo.DateMonthEnds(@TheDate DATETIME)\r\nRETURNS DATETIME AS\r\nBEGIN\r\nRETURN dateadd(ms,-3,dateadd(mm, datediff(mm,0, @TheDate)+1,0))\r\nEND\r\nGO\r\n\r\n-- First day of the quarter\r\nCREATE FUNCTION dbo.DateQuarterStarts(@TheDate DATETIME)\r\nRETURNS DATETIME AS\r\nBEGIN\r\nRETURN dateadd(qq, datediff(qq,0, @TheDate),0)\r\nEND\r\nGO\r\n\r\n-- Last day of the quarter, 3 ms to midnight\r\nCREATE FUNCTION dbo.DateQuarterEnds(@TheDate DATETIME)\r\nRETURNS DATETIME AS\r\nBEGIN\r\nRETURN dateadd(ms,-3,dateadd(qq, datediff(qq,0, @TheDate)+1,0))\r\nEND\r\nGO\r\n\r\n-- First day of the year\r\nCREATE FUNCTION dbo.DateYearStarts(@TheDate DATETIME)\r\nRETURNS DATETIME AS\r\nBEGIN\r\nRETURN dateadd(yy, datediff(yy,0, @TheDate),0)\r\nEND\r\nGO\r\n\r\n-- Last day of the year, 3 ms to midnight\r\nCREATE FUNCTION dbo.DateYearEnds(@TheDate DATETIME)\r\nRETURNS DATETIME AS\r\nBEGIN\r\nRETURN dateadd(ms,-3,dateadd(yy, datediff(yy,0, @TheDate)+1,0))\r\nEND\r\nGO<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The date time data type and the date time functions within SQL are things that I see coming up time and time again in news groups and forums. Questions on how to get rid of the time, how to get&#8230; <a class=\"read-more-button\" href=\"https:\/\/www.sqlinthewild.co.za\/index.php\/2007\/11\/05\/datetime-manipulation\/\">(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":[15,17],"tags":[],"class_list":["post-618","post","type-post","status-publish","format-standard","hentry","category-sql-server","category-t-sql"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p7h6n-9Y","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/posts\/618","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=618"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/posts\/618\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/media?parent=618"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/categories?post=618"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/tags?post=618"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}