﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Article Discussions / Article Discussions by Author / Discuss content posted by Bernabe Diaz  / t-sql first last day date of the month / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Tue, 21 May 2013 04:16:32 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: t-sql first last day date of the month</title><link>http://www.sqlservercentral.com/Forums/Topic818046-1509-1.aspx</link><description>These return different results for the end of months preceding a month that has more days in it.  For instance January has 31 days, and February only 28 or 29 days.Here is an example:DECLARE @d datetime;SET @d = '01/29/2009';-- Last day of monthSELECTdateadd(day,-(datepart(day,@d)),dateadd(mm,1,@d)),  -- Returns 2009-01-30 00:00:00.000, incorrectdateadd(day,-(datepart(day,dateadd(mm,1,@d))),dateadd(mm,1,@d));  -- Returns 2009-01-31 00:00:00.000, correct</description><pubDate>Wed, 02 Dec 2009 11:14:34 GMT</pubDate><dc:creator>benkoskysa</dc:creator></item><item><title>RE: t-sql first last day date of the month</title><link>http://www.sqlservercentral.com/Forums/Topic818046-1509-1.aspx</link><description>The code in my previous post will also strip off the time ;)</description><pubDate>Wed, 02 Dec 2009 06:01:33 GMT</pubDate><dc:creator>Jeff Jordan</dc:creator></item><item><title>RE: t-sql first last day date of the month</title><link>http://www.sqlservercentral.com/Forums/Topic818046-1509-1.aspx</link><description>Hello,if you want to take off hours, minutes and seconds, getting only the "date" part of the calculated date, add this line of code:    SET @R = cast(floor(cast(@R as FLOAT)) as DATETIME)Best wishes,    Francesc</description><pubDate>Wed, 02 Dec 2009 03:31:26 GMT</pubDate><dc:creator>frfernan</dc:creator></item><item><title>RE: t-sql first last day date of the month</title><link>http://www.sqlservercentral.com/Forums/Topic818046-1509-1.aspx</link><description>The other question is why use more variables and code than needed? DECLARE @d SMALLDATETIMESELECT DATEADD(mm, DATEDIFF(mm, 0, ISNULL(@d, GETDATE())), 0)SELECT DATEADD(d, -1, DATEADD(mm, 1 + DATEDIFF(mm, 0, ISNULL(@d, GETDATE())), 0))As a side note, if you're going to do quite a bit of date manipulation you're better off using a calendar table.  There are plenty of examples out there so I won't go into all of that here.  What if you need the first and last date of every month in a period?  You could use a numbers table and date functions but the calendar table is much more efficient.Just my two cents worth ;)</description><pubDate>Tue, 01 Dec 2009 13:10:15 GMT</pubDate><dc:creator>Jeff Jordan</dc:creator></item><item><title>RE: t-sql first last day date of the month</title><link>http://www.sqlservercentral.com/Forums/Topic818046-1509-1.aspx</link><description>So many ways to calculate dates.... :-)within the context of the SP, how about only one select statement to give you both first and last?need to change @FL to 0 = First day and 1 = Last day[code]DECLARE @FL INT, @d AS DATETIMESET @d = ISNULL(@d,GETDATE())SET @FL = 0	-- First day of the monthSELECT DATEADD(mm, @FL + DATEDIFF(mm, 0, @d), 0) - @FLSET @FL = 1	-- Last day of the monthSELECT DATEADD(mm, @FL + DATEDIFF(mm, 0, @d), 0) - @FL[/code]Arkware</description><pubDate>Tue, 01 Dec 2009 06:54:31 GMT</pubDate><dc:creator>ArkWare</dc:creator></item><item><title>RE: t-sql first last day date of the month</title><link>http://www.sqlservercentral.com/Forums/Topic818046-1509-1.aspx</link><description>For the first day of the month, why use the double negative -- why not just      dateadd(day,1-datepart(day,@d),@d)</description><pubDate>Tue, 01 Dec 2009 06:09:35 GMT</pubDate><dc:creator>brian.neumeier-603907</dc:creator></item><item><title>RE: t-sql first last day date of the month</title><link>http://www.sqlservercentral.com/Forums/Topic818046-1509-1.aspx</link><description>A possible alternative is convert/cast with datetime styles (e.g. 112 for ISO in the form of yyyymmdd). This comes in often very handy for date calculations:declare @MyDate datetimeset @MyDate = getdate()-- first day of given month (yyyymm01)select convert(datetime, convert(varchar, (year(@MyDate) * 10000) + (month(@MyDate) * 100) + 1), 112)-- last day of given month (first day of next minus one day -&amp;gt; yyyymm01 + 1 month - 1 day)select dateadd(month, 1, convert(datetime, convert(varchar, (year(@MyDate) * 10000) + (month(@MyDate) * 100) + 1), 112)) - 1Cheers, R.</description><pubDate>Tue, 01 Dec 2009 05:30:33 GMT</pubDate><dc:creator>richard.jereb</dc:creator></item><item><title>RE: t-sql first last day date of the month</title><link>http://www.sqlservercentral.com/Forums/Topic818046-1509-1.aspx</link><description>Hi,I think the Last date can be font by SELECT @R=dateadd(day,-(datepart(day,@d)),dateadd(mm,1,@d))No need to use 		SELECT @R=dateadd(day,-(datepart(day,dateadd(mm,1,@d))),dateadd(mm,1,@d))Because, datepart(day,@d) and datepart(day,dateadd(mm,1,@d)) will return the same result. Then why to add one month to @d?</description><pubDate>Tue, 01 Dec 2009 05:20:55 GMT</pubDate><dc:creator>malu.mn.ktr</dc:creator></item><item><title>t-sql first last day date of the month</title><link>http://www.sqlservercentral.com/Forums/Topic818046-1509-1.aspx</link><description>Comments posted to this topic are about the item [B]&lt;A HREF="/scripts/T-SQL/68735/"&gt;t-sql first last day date of the month&lt;/A&gt;[/B]</description><pubDate>Thu, 12 Nov 2009 12:28:11 GMT</pubDate><dc:creator>diaz.bernabe</dc:creator></item></channel></rss>