{"id":78,"date":"2012-08-21T20:52:00","date_gmt":"2012-08-22T01:52:00","guid":{"rendered":"https:\/\/www.chrystalsander.com\/blog\/?p=78"},"modified":"2023-07-26T12:38:15","modified_gmt":"2023-07-26T17:38:15","slug":"sql-server-get-first-and-last-days-of-month","status":"publish","type":"post","link":"https:\/\/www.chrystalsander.com\/blog\/?p=78","title":{"rendered":"SQL Server:  Get First and Last Days Of Month"},"content":{"rendered":"<div style=\"font-family: &quot;Courier New&quot;,Courier,monospace;\">\n<div>\n<span style=\"color: blue; font-size: small;\">DECLARE<\/span><span style=\"font-size: small;\"> @min <span style=\"color: blue;\">DATETIME<u><\/u><u><\/u><\/span><\/span><\/div>\n<div>\n<span style=\"color: blue; font-size: small;\">DECLARE<\/span><span style=\"font-size: small;\"> @max <span style=\"color: blue;\">DATETIME<u><\/u><u><\/u><\/span><\/span><\/div>\n<div>\n<span style=\"color: blue; font-size: small;\">DECLARE<\/span><span style=\"font-size: small;\"> @now <span style=\"color: blue;\">DATETIME<u><\/u><u><\/u><\/span><\/span><\/div>\n<div>\n<\/div>\n<div>\n<span style=\"color: blue; font-size: small;\">SET<\/span><span style=\"font-size: small;\"> @now <span style=\"color: grey;\">=<\/span> <span style=\"color: magenta;\">GETDATE<\/span><span style=\"color: grey;\">();<\/span><\/span><\/div>\n<div>\n<\/div>\n<p><span style=\"font-size: small;\"><span style=\"font-family: &quot;Courier New&quot;,Courier,monospace;\"><\/span><\/span><span style=\"color: green; font-family: &quot;Courier New&quot;,Courier,monospace; font-size: small;\">&#8211;get first day of last month:<\/span><\/p>\n<div>\n<span style=\"color: blue; font-size: small;\">SET<\/span><span style=\"font-size: small;\"> @min <span style=\"color: grey;\">=<\/span> <span style=\"color: magenta;\">CONVERT<\/span><span style=\"color: grey;\">(<\/span><span style=\"color: blue;\">VARCHAR<\/span><span style=\"color: grey;\">(<\/span>25<span style=\"color: grey;\">),<\/span><span style=\"color: magenta;\">DATEADD<\/span><span style=\"color: grey;\">(<\/span>dd<span style=\"color: grey;\"><wbr><\/wbr><\/span><\/span><\/div>\n<\/div>\n<p><span style=\"font-size: small;\"><span style=\"font-family: &quot;Courier New&quot;,Courier,monospace;\">,-(<\/span><\/span><span style=\"color: magenta; font-family: &quot;Courier New&quot;,Courier,monospace; font-size: small;\">DAY<\/span><span style=\"color: grey; font-family: &quot;Courier New&quot;,Courier,monospace; font-size: small;\">(<\/span><span style=\"color: magenta; font-family: &quot;Courier New&quot;,Courier,monospace; font-size: small;\">DATEADD<\/span><span style=\"color: grey; font-family: &quot;Courier New&quot;,Courier,monospace; font-size: small;\">(<\/span><span style=\"font-size: small;\"><span style=\"font-family: &quot;Courier New&quot;,Courier,monospace;\">mm<\/span><\/span><span style=\"color: grey; font-family: &quot;Courier New&quot;,Courier,monospace; font-size: small;\">,<\/span><span style=\"font-size: small;\"><span style=\"font-family: &quot;Courier New&quot;,Courier,monospace;\">1<\/span><\/span><span style=\"color: grey; font-family: &quot;Courier New&quot;,Courier,monospace; font-size: small;\">,<\/span><span style=\"font-size: small;\"><span style=\"font-family: &quot;Courier New&quot;,Courier,monospace;\">@now<\/span><\/span><span style=\"color: grey; font-family: &quot;Courier New&quot;,Courier,monospace; font-size: small;\">))-<\/span><span style=\"font-size: small;\"><span style=\"font-family: &quot;Courier New&quot;,Courier,monospace;\">1<\/span><\/span><span style=\"color: grey; font-family: &quot;Courier New&quot;,Courier,monospace; font-size: small;\">),<\/span><span style=\"color: magenta; font-family: &quot;Courier New&quot;,Courier,monospace; font-size: small;\"><wbr><\/wbr><\/span><span style=\"font-size: small;\"><span style=\"font-family: &quot;Courier New&quot;,Courier,monospace;\">DATEADD<\/span><\/span><span style=\"color: grey; font-family: &quot;Courier New&quot;,Courier,monospace; font-size: small;\">(<\/span><span style=\"font-size: small;\"><span style=\"font-family: &quot;Courier New&quot;,Courier,monospace;\">mm<\/span><\/span><span style=\"color: grey; font-family: &quot;Courier New&quot;,Courier,monospace; font-size: small;\">,-<\/span><span style=\"font-size: small;\"><span style=\"font-family: &quot;Courier New&quot;,Courier,monospace;\">1<\/span><\/span><span style=\"color: grey; font-family: &quot;Courier New&quot;,Courier,monospace; font-size: small;\">,<\/span><span style=\"font-size: small;\"><span style=\"font-family: &quot;Courier New&quot;,Courier,monospace;\">@now<\/span><\/span><span style=\"color: grey; font-family: &quot;Courier New&quot;,Courier,monospace; font-size: small;\">)),<\/span><span style=\"font-size: small;\"><span style=\"font-family: &quot;Courier New&quot;,Courier,monospace;\">101<\/span><\/span><span style=\"color: grey; font-family: &quot;Courier New&quot;,Courier,monospace; font-size: small;\">)<\/span><span style=\"font-size: small;\"><span style=\"font-family: &quot;Courier New&quot;,Courier,monospace;\">&nbsp;<\/span><\/span><\/p>\n<p><span style=\"color: green; font-family: &quot;Courier New&quot;,Courier,monospace; font-size: small;\">&#8211;get the last day of last month<\/span><\/p>\n<div style=\"font-family: &quot;Courier New&quot;,Courier,monospace;\">\n<span style=\"color: blue; font-size: small;\">SET<\/span><span style=\"font-size: small;\"> @max <span style=\"color: grey;\">=<\/span> <span style=\"color: magenta;\">CONVERT<\/span><span style=\"color: grey;\">(<\/span><span style=\"color: blue;\">VARCHAR<\/span><span style=\"color: grey;\">(<\/span>25<span style=\"color: grey;\">),<\/span><span style=\"color: magenta;\">DATEADD<\/span><span style=\"color: grey;\">(<\/span>dd<span style=\"color: grey;\"><wbr><\/wbr><\/span><\/span><\/div>\n<p><span style=\"font-size: small;\"><span style=\"font-family: &quot;Courier New&quot;,Courier,monospace;\">,-(<\/span><\/span><span style=\"color: magenta; font-family: &quot;Courier New&quot;,Courier,monospace; font-size: small;\">DAY<\/span><span style=\"color: grey; font-family: &quot;Courier New&quot;,Courier,monospace; font-size: small;\">(<\/span><span style=\"font-size: small;\"><span style=\"font-family: &quot;Courier New&quot;,Courier,monospace;\">@now<\/span><\/span><span style=\"color: grey; font-family: &quot;Courier New&quot;,Courier,monospace; font-size: small;\">)),<\/span><span style=\"font-size: small;\"><span style=\"font-family: &quot;Courier New&quot;,Courier,monospace;\">@now<\/span><\/span><span style=\"color: grey; font-family: &quot;Courier New&quot;,Courier,monospace; font-size: small;\">),<\/span><span style=\"font-size: small;\"><span style=\"font-family: &quot;Courier New&quot;,Courier,monospace;\">101<\/span><\/span><span style=\"color: grey; font-family: &quot;Courier New&quot;,Courier,monospace; font-size: small;\">)<\/span><span style=\"font-size: small;\"><span style=\"font-family: &quot;Courier New&quot;,Courier,monospace;\">&nbsp;<\/span><\/span><\/p>\n<div style=\"font-family: &quot;Courier New&quot;,Courier,monospace;\">\n<\/div>\n<div style=\"font-family: &quot;Courier New&quot;,Courier,monospace;\">\n<span style=\"color: green; font-family: &quot;Courier New&quot;,Courier,monospace; font-size: small;\">&#8211;get the first day of current month<\/span><span style=\"color: green; font-size: small;\"> <\/span><\/div>\n<div style=\"font-family: &quot;Courier New&quot;,Courier,monospace;\">\n<span style=\"color: blue; font-size: small;\">SET<\/span><span style=\"font-size: small;\"> @min <span style=\"color: grey;\">=<\/span> <span style=\"color: magenta;\">CONVERT<\/span><span style=\"color: grey;\">(<\/span><span style=\"color: blue;\">VARCHAR<\/span><span style=\"color: grey;\">(<\/span>25<span style=\"color: grey;\">),<\/span><span style=\"color: magenta;\">DATEADD<\/span><span style=\"color: grey;\">(<\/span>dd<span style=\"color: grey;\"><wbr><\/wbr><\/span><\/span><\/div>\n<p><span style=\"font-size: small;\"><span style=\"font-family: &quot;Courier New&quot;,Courier,monospace;\">,-(<\/span><\/span><span style=\"color: magenta; font-family: &quot;Courier New&quot;,Courier,monospace; font-size: small;\">DAY<\/span><span style=\"color: grey; font-family: &quot;Courier New&quot;,Courier,monospace; font-size: small;\">(<\/span><span style=\"font-size: small;\"><span style=\"font-family: &quot;Courier New&quot;,Courier,monospace;\">@now<\/span><\/span><span style=\"color: grey; font-family: &quot;Courier New&quot;,Courier,monospace; font-size: small;\">)-<\/span><span style=\"font-size: small;\"><span style=\"font-family: &quot;Courier New&quot;,Courier,monospace;\">1<\/span><\/span><span style=\"color: grey; font-family: &quot;Courier New&quot;,Courier,monospace; font-size: small;\">),<\/span><span style=\"font-size: small;\"><span style=\"font-family: &quot;Courier New&quot;,Courier,monospace;\">@now<\/span><\/span><span style=\"color: grey; font-family: &quot;Courier New&quot;,Courier,monospace; font-size: small;\">),<\/span><span style=\"font-size: small;\"><span style=\"font-family: &quot;Courier New&quot;,Courier,monospace;\">101<\/span><\/span><span style=\"color: grey; font-family: &quot;Courier New&quot;,Courier,monospace; font-size: small;\">)<\/span><span style=\"font-size: small;\"><span style=\"font-family: &quot;Courier New&quot;,Courier,monospace;\">&nbsp;&nbsp;<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"font-family: &quot;Courier New&quot;,Courier,monospace;\"><\/span><\/span><span style=\"color: green; font-family: &quot;Courier New&quot;,Courier,monospace; font-size: small;\">&#8211;get the last day of current month<\/span><span style=\"font-size: small;\"><span style=\"font-family: &quot;Courier New&quot;,Courier,monospace;\"> <\/span><\/span><\/p>\n<div style=\"font-family: &quot;Courier New&quot;,Courier,monospace;\">\n<span style=\"color: blue; font-size: small;\">SET<\/span><span style=\"font-size: small;\"> @max <span style=\"color: grey;\">=<\/span> <span style=\"color: magenta;\">CONVERT<\/span><span style=\"color: grey;\">(<\/span><span style=\"color: blue;\">VARCHAR<\/span><span style=\"color: grey;\">(<\/span>25<span style=\"color: grey;\">),<\/span><span style=\"color: magenta;\">DATEADD<\/span><span style=\"color: grey;\">(<\/span>dd<span style=\"color: grey;\"><wbr><\/wbr><\/span><\/span><\/div>\n<p><span style=\"font-size: small;\"><span style=\"font-family: &quot;Courier New&quot;,Courier,monospace;\">,-(<\/span><\/span><span style=\"color: magenta; font-family: &quot;Courier New&quot;,Courier,monospace; font-size: small;\">DAY<\/span><span style=\"color: grey; font-family: &quot;Courier New&quot;,Courier,monospace; font-size: small;\">(<\/span><span style=\"color: magenta; font-family: &quot;Courier New&quot;,Courier,monospace; font-size: small;\">DATEADD<\/span><span style=\"color: grey; font-family: &quot;Courier New&quot;,Courier,monospace; font-size: small;\">(<\/span><span style=\"font-size: small;\"><span style=\"font-family: &quot;Courier New&quot;,Courier,monospace;\">mm<\/span><\/span><span style=\"color: grey; font-family: &quot;Courier New&quot;,Courier,monospace; font-size: small;\">,<\/span><span style=\"font-size: small;\"><span style=\"font-family: &quot;Courier New&quot;,Courier,monospace;\">1<\/span><\/span><span style=\"color: grey; font-family: &quot;Courier New&quot;,Courier,monospace; font-size: small;\">,<\/span><span style=\"font-size: small;\"><span style=\"font-family: &quot;Courier New&quot;,Courier,monospace;\">@now<\/span><\/span><span style=\"color: grey; font-family: &quot;Courier New&quot;,Courier,monospace; font-size: small;\">))),<\/span><span style=\"color: magenta; font-family: &quot;Courier New&quot;,Courier,monospace; font-size: small;\">DA<wbr><\/wbr><\/span><span style=\"font-size: small;\"><span style=\"font-family: &quot;Courier New&quot;,Courier,monospace;\">TEADD<\/span><\/span><span style=\"color: grey; font-family: &quot;Courier New&quot;,Courier,monospace; font-size: small;\">(<\/span><span style=\"font-size: small;\"><span style=\"font-family: &quot;Courier New&quot;,Courier,monospace;\">mm<\/span><\/span><span style=\"color: grey; font-family: &quot;Courier New&quot;,Courier,monospace; font-size: small;\">,<\/span><span style=\"font-size: small;\"><span style=\"font-family: &quot;Courier New&quot;,Courier,monospace;\">1<\/span><\/span><span style=\"color: grey; font-family: &quot;Courier New&quot;,Courier,monospace; font-size: small;\">,<\/span><span style=\"font-size: small;\"><span style=\"font-family: &quot;Courier New&quot;,Courier,monospace;\">@now<\/span><\/span><span style=\"color: grey; font-family: &quot;Courier New&quot;,Courier,monospace; font-size: small;\">)),<\/span><span style=\"font-size: small;\"><span style=\"font-family: &quot;Courier New&quot;,Courier,monospace;\">101<\/span><\/span><span style=\"color: grey; font-family: &quot;Courier New&quot;,Courier,monospace; font-size: small;\">)<\/span><span style=\"font-size: small;\"><span style=\"font-family: &quot;Courier New&quot;,Courier,monospace;\">&nbsp;<\/span><\/span><span style=\"color: green; font-family: &quot;Courier New&quot;,Courier,monospace; font-size: small;\"><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>DECLARE @min DATETIME DECLARE @max DATETIME DECLARE @now DATETIME SET @now = GETDATE(); &#8211;get first day of last month: SET @min = CONVERT(VARCHAR(25),DATEADD(dd ,-(DAY(DATEADD(mm,1,@now))-1),DATEADD(mm,-1,@now)),101)&nbsp; &#8211;get the last day of last month SET @max = CONVERT(VARCHAR(25),DATEADD(dd ,-(DAY(@now)),@now),101)&nbsp; &#8211;get the first day of current month SET @min = CONVERT(VARCHAR(25),DATEADD(dd ,-(DAY(@now)-1),@now),101)&nbsp;&nbsp; &#8211;get the last day of current month &hellip; <a href=\"https:\/\/www.chrystalsander.com\/blog\/?p=78\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;SQL Server:  Get First and Last Days Of Month&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[50],"_links":{"self":[{"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/78"}],"collection":[{"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=78"}],"version-history":[{"count":1,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/78\/revisions"}],"predecessor-version":[{"id":194,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/78\/revisions\/194"}],"wp:attachment":[{"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=78"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=78"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=78"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}