Recent PostsRecent Posts Popular TopicsPopular Topics
 Home Search Members Calendar Who's On

 Date Puzzle Rate Topic Display Mode Topic Options
Author
 Message
 Posted Thursday, October 28, 2010 12:36 PM
 Hall of Fame Group: General Forum Members Last Login: Saturday, October 24, 2015 2:31 AM Points: 3,158, Visits: 11,771
 Dan Guzman - Not the MVP (10/28/2010)Michael Valentine Jones (10/27/2010)Dan Guzman - Not the MVP (10/27/2010)So, is this the simplest and quickest way to get the last day of any month?DATEADD(MONTH,x,'2010-Jan-01') Where x is the month you are looking for the last day.I've seen a lot of code over the years that performs some sort of find the first day of the month after the month in question, then subtract 1 day. Seems totally unnecessary with this function.Not sure if you mis-typed that, but it is fairly obvious that will return the FIRST day of the month AFTER x. For example, if x=10, it will return 2010-11-01 00:00:00.000.This is the standard and most efficient way of getting the last day of the month a particular date is in:`select LastDay = dateadd(mm,datediff(mm,-1,getdate()),-1) `Result:`LastDay-----------------------2010-10-31 00:00:00.000`You are correct, I meant January 31st. so:DATEADD(MONTH,x-1,'2010-Jan-31') so that x is the month your looking for,or for last day of the current month:DATEADD(MONTH,MONTH(GETDATE())-1,'2010-Jan-31') I even thinkDATEADD(MONTH,MONTH(GETDATE())-1,CAST(YEAR(GETDATE()) AS VARCHAR(4)) +'-Jan-31') is easier to read than the one you mention, with the DATEDIFF, but that's just me.I think it’s debatable that the code you posted is easier to read, especially since is much longer. Maybe what you really mean is that you find it easier to understand. I have to disagree, especially since the method I posted is so easy to adapt to similar problems: `select Description = 'Getdate()', DT = getdate() union allselect 'Start of Day', DT = dateadd(dd,datediff(dd, 0,getdate()), 0) union allselect 'Start of Yesterday', DT = dateadd(dd,datediff(dd, 0,getdate())-1, 0) union allselect 'Start of Next Day', DT = dateadd(dd,datediff(dd, 0,getdate())+1, 0) union allselect 'First Day of Month', DT = dateadd(mm,datediff(mm, 0,getdate()), 0) union allselect 'Last Day of Month', DT = dateadd(mm,datediff(mm,-1,getdate()),-1) union allselect 'First Day of Last Month', DT = dateadd(mm,datediff(mm, 0,getdate())-1, 0) union allselect 'Last Day of Last Month', DT = dateadd(mm,datediff(mm,-1,getdate())-1,-1) union allselect 'First Day of Next Month', DT = dateadd(mm,datediff(mm, 0,getdate())+1, 0) union allselect 'Last Day of Next Month', DT = dateadd(mm,datediff(mm,-1,getdate())+1,-1) union allselect 'First Day of Quarter', DT = dateadd(qq,datediff(qq, 0,getdate()), 0) union allselect 'Last Day of Quarter', DT = dateadd(qq,datediff(qq,-1,getdate()),-1) union allselect 'First Day of Year', DT = dateadd(yy,datediff(yy, 0,getdate()), 0) union allselect 'Last Day of Year', DT = dateadd(yy,datediff(yy,-1,getdate()),-1) union allselect 'First Day of Last Year', DT = dateadd(yy,datediff(yy, 0,getdate())-1, 0) union allselect 'Last Day of Last Year', DT = dateadd(yy,datediff(yy,-1,getdate())-1,-1) union allselect 'First Day of Next Year', DT = dateadd(yy,datediff(yy, 0,getdate())+1, 0) union allselect 'Last Day of Next Year', DT = dateadd(yy,datediff(yy,-1,getdate())+1,-1)`Results:`Description DT----------------------- -----------------------Getdate() 2010-10-28 14:27:12.620Start of Day 2010-10-28 00:00:00.000Start of Yesterday 2010-10-27 00:00:00.000Start of Next Day 2010-10-29 00:00:00.000First Day of Month 2010-10-01 00:00:00.000Last Day of Month 2010-10-31 00:00:00.000First Day of Last Month 2010-09-01 00:00:00.000Last Day of Last Month 2010-09-30 00:00:00.000First Day of Next Month 2010-11-01 00:00:00.000Last Day of Next Month 2010-11-30 00:00:00.000First Day of Quarter 2010-10-01 00:00:00.000Last Day of Quarter 2010-12-31 00:00:00.000First Day of Year 2010-01-01 00:00:00.000Last Day of Year 2010-12-31 00:00:00.000First Day of Last Year 2009-01-01 00:00:00.000Last Day of Last Year 2009-12-31 00:00:00.000First Day of Next Year 2011-01-01 00:00:00.000Last Day of Next Year 2011-12-31 00:00:00.000`Extensive testing has shown that date manipulations involving conversion to character strings are much less efficient than the code I posted. Also, that code does not work with settings of language that are non-english, so at the very least, you should change the code to this:`select dateadd(month,month(getdate())-1,cast(year(getdate()) as varchar(4)) +'0131')`
Post #1012574
 Posted Sunday, October 31, 2010 8:21 PM
 SSChasing Mays Group: General Forum Members Last Login: Sunday, November 17, 2013 11:53 AM Points: 623, Visits: 237
 Simple & Easy
Post #1013657
 Posted Tuesday, November 2, 2010 10:22 AM
 SSC Veteran Group: General Forum Members Last Login: Tuesday, August 25, 2015 11:50 AM Points: 218, Visits: 124
 Accurate question. I think any question that uses a date with obvious notation to remove all ambiguity such as '2010-Jan-01' is fine. We clearly understand which is the year, month, and day. '201000205' while most would consider it to be February 5, 2010; 5 February 2010; or 2010 Februrary 5 it isn't perfectly clear.I would have liked to seen this instead:DATEADD(MONTH, 1, '2010-Jan-30')Feb 28 is correct not Feb 27 day preceeding end of month nor March 1 30 days post Jan 30.Tim
Post #1014656
 Posted Tuesday, November 2, 2010 10:31 AM
 Hall of Fame Group: General Forum Members Last Login: Saturday, October 24, 2015 2:31 AM Points: 3,158, Visits: 11,771
 timothy bates (11/2/2010)Accurate question. I think any question that uses a date with obvious notation to remove all ambiguity such as '2010-Jan-01' is fine. We clearly understand which is the year, month, and day. '201000205' while most would consider it to be February 5, 2010; 5 February 2010; or 2010 Februrary 5 it isn't perfectly clear.I would have liked to seen this instead:DATEADD(MONTH, 1, '2010-Jan-30')Feb 28 is correct not Feb 27 day preceeding end of month nor March 1 30 days post Jan 30.TimThe problem with '2010-Jan-01' is that it is not even a valid date with many or most language settings.`set language italian;select DT = convert(datetime,'2010-Jan-01')`Result:`L'impostazione della lingua è stata sostituita con Italiano.Server: Msg 241, Level 16, State 1, Line 3Conversione non riuscita durante la conversione di una stringa di caratteri nel tipo di dati datetime.`'201000205' is perfectly clear to SQL Server. It will only interpret it one way, as YYYYMMDD, no matter what the setting of LANGUAGE or DATEFORMAT.
Post #1014663
 Posted Tuesday, November 2, 2010 10:55 AM
 SSC Veteran Group: General Forum Members Last Login: Tuesday, August 25, 2015 11:50 AM Points: 218, Visits: 124
 Michael Valentine Jones (11/2/2010)timothy bates (11/2/2010)Accurate question. I think any question that uses a date with obvious notation to remove all ambiguity such as '2010-Jan-01' is fine. We clearly understand which is the year, month, and day. '201000205' while most would consider it to be February 5, 2010; 5 February 2010; or 2010 Februrary 5 it isn't perfectly clear.I would have liked to seen this instead:DATEADD(MONTH, 1, '2010-Jan-30')Feb 28 is correct not Feb 27 day preceeding end of month nor March 1 30 days post Jan 30.TimThe problem with '2010-Jan-01' is that it is not even a valid date with many or most language settings.`set language italian;select DT = convert(datetime,'2010-Jan-01')`Result:`L'impostazione della lingua è stata sostituita con Italiano.Server: Msg 241, Level 16, State 1, Line 3Conversione non riuscita durante la conversione di una stringa di caratteri nel tipo di dati datetime.`'201000205' is perfectly clear to SQL Server. It will only interpret it one way, as YYYYMMDD, no matter what the setting of LANGUAGE or DATEFORMAT.Actually there is an error. One too many 0's between the 1 and second 2 (mine but I am getting picky now).As long as the intent is clear and ambiguity can't be inferred I am fine with however it is posted. I don't execute QOTD questions to determine my answer. I work through it as though I am reviewing a printout of code or taking an exam. I assume (wrongly at times) that dates are shown to be compliant with the posters environment but may not be with mine. If someone posted a question with a date '2010-Juli-06'. I would assume it works with the local install. I haven't had to account for backup/failover servers installed in separate countries as part of a disaster recovery/business continuity plan (yet). Of course this could be a whole separate question.Maybe all questions should include the Set Language command to remove all possible ambiguity or something like:declare @dt as datetimeselect @dt = select dateadd(mm,2,@dt) as two_months_laterTim
Post #1014687
 Posted Tuesday, November 2, 2010 2:29 PM
 SSC-Insane Group: General Forum Members Last Login: Monday, November 21, 2016 11:03 AM Points: 20,009, Visits: 18,255
 Thanks for the question. Jason AKA CirqueDeSQLeilI have given a name to my pain...MCM SQL Server, MVPSQL RNNRPosting Performance Based Questions - Gail Shaw
Post #1014893
 Posted Thursday, November 10, 2011 3:54 PM
 Mr or Mrs. 500 Group: General Forum Members Last Login: Monday, October 17, 2016 8:53 AM Points: 539, Visits: 1,029
 Yes, of course, it is only a 30 day month. Albeit, it is not because it knows the "END" of the month.. but the requested day is the 31st of the month.. adding 3 to the month will take it up to April and then calculates which is the highest possible day in that month is and if it is less than 31 then show that .. in this case 30.. otherwise will return the requested day.. Cheers,John Esraelo
Post #1203868
 Posted Thursday, November 10, 2011 3:55 PM
 Mr or Mrs. 500 Group: General Forum Members Last Login: Monday, October 17, 2016 8:53 AM Points: 539, Visits: 1,029
 Of course the language notion is a very good point. however, this example was of course set for English .. but your point is well taken Cheers,John Esraelo
Post #1203870

 Permissions