May 9, 2011 at 11:15 pm
Comments posted to this topic are about the item Find Last Day of the Month
May 10, 2011 at 12:19 am
Another way to do this :
1) Add 1 month
2) Substract the day number of the previous result :
DECLARE @mydate datetime='20080224'
SELECT DATEADD(day, -DATEPART(day, @Mydate), DATEADD(MONTH, 1, @mydate))
May 10, 2011 at 12:44 am
f.marquis (5/10/2011)
Another way to do this :1) Add 1 month
2) Substract the day number of the previous result :
DECLARE @mydate datetime='20080224'
SELECT DATEADD(day, -DATEPART(day, @Mydate), DATEADD(MONTH, 1, @mydate))
One problem with this is... it doesnt work. Run the code for '20080331' and it returns '20080330'.
/T
May 10, 2011 at 12:58 am
You're right :
I think I wrote the good instructions but a wrong SQL.
This might be better :
1) Add 1 month
2) Substract the day number of the previousresult :
DECLARE @mydate datetime='20080331'
SELECT DATEADD( day, -DATEPART(day, DATEADD(MONTH, 1, @mydate)), DATEADD(MONTH, 1, @mydate))
May 10, 2011 at 12:58 am
A quick search on this site and one could for instance find
http://www.sqlservercentral.com/Forums/Topic777289-1291-1.aspx
Scroll down to Lynn Pettis first post. I would recommend reading the whole post since there is also some interesting posts on the following pages.
/T
May 10, 2011 at 3:59 am
Below is a one line statement to compute last date of the month for a given input date.
DECLARE @MyDate datetime
SET @MyDate = '2012/11/25'
SELECT DATEADD(DAY,DATEPART(DAY,DATEADD(MONTH,1,@MyDate))*-1,DATEADD(MONTH,1,@MyDate))
--Output result: 2012-11-30 00:00:00.000
May 10, 2011 at 4:47 am
select dateadd(d,-1,left(CONVERT(varchar,CURRENT_TIMESTAMP,112),6)+'01')
May 10, 2011 at 4:50 am
good one!
May 10, 2011 at 4:54 am
This one will do the trick with just three instructions (and no string conversions which are best avoided):
SELECT
"LastDayOfMonth" = DatePart(day, DateAdd(month, DateDiff(month, -1, GetDate()), -1))
May 10, 2011 at 6:29 am
I prefer this statement
DECLARE @myDate DATETIME
SELECT @myDate = GETDATE()
SELECT DATEADD(dd, -(DAY(DATEADD(mm, 1, @myDate))), DATEADD(mm, 1, @myDate))
And then to get the date without the time just plug it in here:
SELECT DATEADD(dd, 0, DATEDIFF(dd, 0, @myDate))
May 10, 2011 at 7:34 am
Use this to get the last day of the month
DECLARE @date DATETIME
SELECT @date='2011/05/10'
SELECT DATEADD(DD, -1, DATEADD(MM, DATEDIFF(MM,0,@date)+1,0))
May 11, 2011 at 1:01 pm
wallah!???
I think you mean voilà
A wallah is a servant...
May 16, 2011 at 7:40 am
SELECT DATEADD(MM,DATEDIFF(MM,-1,getdate()),0)-1
Regards,
Mitesh OSwal
+918698619998
May 19, 2011 at 6:50 am
--Last day of the current month.
--You could format it and it also takes into account the timestamp - 3 milliseconds
SELECT DATEADD(MS,-3,DATEADD(MM, DATEDIFF(M,0,GETDATE() )+1, 0))
June 21, 2011 at 7:22 am
DateTime already has this implemented...
DateTime.DaysInMonth(Year as Int32, Month as Int32)
Viewing 15 posts - 1 through 15 (of 15 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy