## Date Puzzle

 Author Message Michael Valentine Jones SSChampion Group: General Forum Members Points: 14455 Visits: 11848 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')` Dhruvesh Shah Right there with Babe Group: General Forum Members Points: 799 Visits: 237 Simple & Easy timothy bates SSC Veteran Group: General Forum Members Points: 252 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 Michael Valentine Jones SSChampion Group: General Forum Members Points: 14455 Visits: 11848 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. timothy bates SSC Veteran Group: General Forum Members Points: 252 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 SQLRNNR SSC Guru Group: General Forum Members Points: 66553 Visits: 18570 Thanks for the question. Jason AKA CirqueDeSQLeilI have given a name to my pain...MCM SQL Server, MVPSQL RNNRPosting Performance Based Questions - Gail Shaw John Esraelo-498130 SSCommitted Group: General Forum Members Points: 1551 Visits: 1030 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 John Esraelo-498130 SSCommitted Group: General Forum Members Points: 1551 Visits: 1030 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

## Permissions

 You can't post new topics. You can't post topic replies. You can't post new polls. You can't post replies to polls. You can't edit your own topics. You can't delete your own topics. You can't edit other topics. You can't delete other topics. You can't edit your own posts. You can't edit other posts. You can't delete your own posts. You can't delete other posts. You can't post events. You can't edit your own events. You can't edit other events. You can't delete your own events. You can't delete other events. You can't send private messages. You can't send emails. You can read topics. You can't vote in polls. You can't upload attachments. You can download attachments. You can't post HTML code. You can't edit HTML code. You can't post IFCode. You can't post JavaScript. You can post emoticons. You can't post or upload images.

##### Select a forum
 SQL Server 2016      SQL Server 2016 - Administration      SQL Server 2016 - Development and T-SQL SQL Server 2014      Administration - SQL Server 2014      Development - SQL Server 2014 SQL Server 2012      SQL 2012 - General      SQL Server 2012 - T-SQL SQL Server vNext      SQL Server 14 - Administration      SQL Server 14 - Development SQL Server 2008      SQL Server 2008 - General      T-SQL (SS2K8)      June 2007 CTP      Working with Oracle      July CTP      SQL Server Newbies      Security (SS2K8)      SQL Server 2008 High Availability      SQL Server 2008 Administration      Data Corruption (SS2K8 / SS2K8 R2)      SQL Server 2008 Performance Tuning Cloud Computing      SQL Azure - Development      SQL Azure - Administration      Amazon AWS and other cloud vendors      General Cloud Computing Questions Reporting Services      Reporting Services      Reporting Services 2005 Administration      Reporting Services 2005 Development      Reporting Services 2008/R2 Administration      Reporting Services 2008 Development      SSRS 2012      SSRS 2014      SSRS 2016 Programming      Connecting      General      SMO/RMO/DMO      XML      Service Broker      Powershell      Testing      TFS/Data Dude/DBPro      SSDT      Continuous Integration, Deployment, and Delivery      R Services and R Language Data Warehousing      Integration Services      Strategies and Ideas      Analysis Services      Data Transformation Services (DTS)      Performance Point      Data Mining      PowerPivot      R language      Machine Learning Database Design      Disaster Recovery      Design Ideas and Questions      Relational Theory      Hardware      Virtualization SQLServerCentral.com      Anything that is NOT about SQL!      Contests!      Editorials      SQLServerCentral.com Announcements      SQLServerCentral.com Website Issues      Suggestions      Tag Issues with Content      Podcast Feedback      SQLServerCentral.com Test Forum      Articles Requested SQL Server 2005      Administering      Backups      Business Intelligence      CLR Integration and Programming.      Data Corruption      Development      Working with Oracle      SQL Server 2005 Compact Edition      SQL Server 2005 General Discussion      SQL Server 2005 Security      SQL Server 2005 Strategies      SS2K5 Replication      SQL Server Express      SQL Server 2005 Performance Tuning      SQL Server 2005 Integration Services      T-SQL (SS2K5)      SQL Server Newbies SQL Server 7,2000      Administration      Backups      Data Corruption      General      Globalization      In The Enterprise      Working with Oracle      Security      Strategies      SQL Server Newbies      Service Packs      SQL Server CE      Performance Tuning      Replication      Sarbanes-Oxley      T-SQL      SQL Server Agent SQL Server and other platforms      MySQL      Oracle      PostgreSQL      DB2      SQL Server and Sharepoint Older Versions of SQL (v6.5, v6.0, v4.2)      Older Versions of SQL (v6.5, v6.0, v4.2) Career      Certification      Employers and Employees      Events      Job Postings      Resumes and Job Hunters      Presentations and Speaking      Retired Members Testing Center      Question of the Day (QOD)      SQL Server Security Skills Microsoft Access      Microsoft Access Products and Books      Third Party Products         CA         Extreme Technologies.         Innovartis         Embarcadero         SQL Sentry         Sonasoft         SQLCentric         Golden Gate Software         Lumigent         Red Gate Software         Quest Software         ApexSQL         Idera      Discussions about Books         Discuss Programming Books          Discuss XML Books          Discuss T-SQL Books          Discuss Data Warehousing Books          Discuss DTS Books          Discuss SQL Server 7.0 Books         Discuss SQL Server 2000 Books Notification Services      Administration Article Discussions Future Versions      SQL 12