{"id":239,"date":"2011-04-21T01:13:35","date_gmt":"2011-04-20T20:13:35","guid":{"rendered":"http:\/\/kennywu.info\/?p=239"},"modified":"2011-04-23T07:30:05","modified_gmt":"2011-04-23T02:30:05","slug":"essential-sql-server-date-time-functions","status":"publish","type":"post","link":"http:\/\/kennywu.info\/?p=239","title":{"rendered":"Essential SQL Server Date Time Functions"},"content":{"rendered":"<h4> Returns a datetime value for the specified year, month and day<\/h4>\n<pre lang=\"sql\">\r\ncreate function [dbo].[ReturnDate](@Year int, @Month int, @Day int) returns datetime\r\nas\r\n    begin\r\n    return dateadd(month,((@Year-1900)*12)+@Month-1,@Day-1)\r\n    end\r\n<\/pre>\n<h4>Returns @DateTime at midnight; i.e., it removes the time portion of a DateTime value. <\/h4>\n<pre lang=\"sql\">\r\ncreate  function DateOnly(@DateTime DateTime)\r\nreturns datetime\r\nas\r\n    begin\r\n    return dateadd(dd,0, datediff(dd,0,@DateTime))\r\n    end\r\n<\/pre>\n<p>Sample:<\/p>\n<pre lang=\"sql\">\r\nselect dbo.ReturnDate(year(getdate()), month(getdate()),1) -- returns the first day of the current month.\r\nselect dbo.ReturnDate(year(getdate()), month(getdate())+1,0) -- returns the last day of the current month.\r\n<\/pre>\n<p>Go to there for details:<br \/>\n<a href=\"http:\/\/weblogs.sqlteam.com\/jeffs\/archive\/2007\/01\/02\/56079.aspx\">http:\/\/weblogs.sqlteam.com\/jeffs\/archive\/2007\/01\/02\/56079.aspx<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Returns a datetime value for the specified year, month and day create function [dbo].[ReturnDate](@Year int, @Month int, @Day int) returns datetime as begin return dateadd(month,((@Year-1900)*12)+@Month-1,@Day-1) end Returns @DateTime at midnight; i.e., it removes the time portion of a DateTime value. &hellip; <a href=\"http:\/\/kennywu.info\/?p=239\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-239","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"http:\/\/kennywu.info\/index.php?rest_route=\/wp\/v2\/posts\/239","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/kennywu.info\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/kennywu.info\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/kennywu.info\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/kennywu.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=239"}],"version-history":[{"count":14,"href":"http:\/\/kennywu.info\/index.php?rest_route=\/wp\/v2\/posts\/239\/revisions"}],"predecessor-version":[{"id":254,"href":"http:\/\/kennywu.info\/index.php?rest_route=\/wp\/v2\/posts\/239\/revisions\/254"}],"wp:attachment":[{"href":"http:\/\/kennywu.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=239"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/kennywu.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=239"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/kennywu.info\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=239"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}