Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Date Puzzle


Date Puzzle

Author
Message
Michael Valentine Jones
Michael Valentine Jones
Hall of Fame
Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)

Group: General Forum Members
Points: 3254 Visits: 11771
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 think

DATEADD(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 all
select 'Start of Day', DT = dateadd(dd,datediff(dd, 0,getdate()), 0) union all
select 'Start of Yesterday', DT = dateadd(dd,datediff(dd, 0,getdate())-1, 0) union all
select 'Start of Next Day', DT = dateadd(dd,datediff(dd, 0,getdate())+1, 0) union all
select 'First Day of Month', DT = dateadd(mm,datediff(mm, 0,getdate()), 0) union all
select 'Last Day of Month', DT = dateadd(mm,datediff(mm,-1,getdate()),-1) union all
select 'First Day of Last Month', DT = dateadd(mm,datediff(mm, 0,getdate())-1, 0) union all
select 'Last Day of Last Month', DT = dateadd(mm,datediff(mm,-1,getdate())-1,-1) union all
select 'First Day of Next Month', DT = dateadd(mm,datediff(mm, 0,getdate())+1, 0) union all
select 'Last Day of Next Month', DT = dateadd(mm,datediff(mm,-1,getdate())+1,-1) union all
select 'First Day of Quarter', DT = dateadd(qq,datediff(qq, 0,getdate()), 0) union all
select 'Last Day of Quarter', DT = dateadd(qq,datediff(qq,-1,getdate()),-1) union all
select 'First Day of Year', DT = dateadd(yy,datediff(yy, 0,getdate()), 0) union all
select 'Last Day of Year', DT = dateadd(yy,datediff(yy,-1,getdate()),-1) union all
select 'First Day of Last Year', DT = dateadd(yy,datediff(yy, 0,getdate())-1, 0) union all
select 'Last Day of Last Year', DT = dateadd(yy,datediff(yy,-1,getdate())-1,-1) union all
select 'First Day of Next Year', DT = dateadd(yy,datediff(yy, 0,getdate())+1, 0) union all
select 'Last Day of Next Year', DT = dateadd(yy,datediff(yy,-1,getdate())+1,-1)




Results:
Description             DT
----------------------- -----------------------
Getdate() 2010-10-28 14:27:12.620
Start of Day 2010-10-28 00:00:00.000
Start of Yesterday 2010-10-27 00:00:00.000
Start of Next Day 2010-10-29 00:00:00.000
First Day of Month 2010-10-01 00:00:00.000
Last Day of Month 2010-10-31 00:00:00.000
First Day of Last Month 2010-09-01 00:00:00.000
Last Day of Last Month 2010-09-30 00:00:00.000
First Day of Next Month 2010-11-01 00:00:00.000
Last Day of Next Month 2010-11-30 00:00:00.000
First Day of Quarter 2010-10-01 00:00:00.000
Last Day of Quarter 2010-12-31 00:00:00.000
First Day of Year 2010-01-01 00:00:00.000
Last Day of Year 2010-12-31 00:00:00.000
First Day of Last Year 2009-01-01 00:00:00.000
Last Day of Last Year 2009-12-31 00:00:00.000
First Day of Next Year 2011-01-01 00:00:00.000
Last 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
Dhruvesh Shah
SSChasing Mays
SSChasing Mays (635 reputation)SSChasing Mays (635 reputation)SSChasing Mays (635 reputation)SSChasing Mays (635 reputation)SSChasing Mays (635 reputation)SSChasing Mays (635 reputation)SSChasing Mays (635 reputation)SSChasing Mays (635 reputation)

Group: General Forum Members
Points: 635 Visits: 237
Simple & Easy
timothy bates
timothy bates
SSC Veteran
SSC Veteran (218 reputation)SSC Veteran (218 reputation)SSC Veteran (218 reputation)SSC Veteran (218 reputation)SSC Veteran (218 reputation)SSC Veteran (218 reputation)SSC Veteran (218 reputation)SSC Veteran (218 reputation)

Group: General Forum Members
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
Michael Valentine Jones
Michael Valentine Jones
Hall of Fame
Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)

Group: General Forum Members
Points: 3254 Visits: 11771
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.

Tim


The 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 3
Conversione 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
timothy bates
SSC Veteran
SSC Veteran (218 reputation)SSC Veteran (218 reputation)SSC Veteran (218 reputation)SSC Veteran (218 reputation)SSC Veteran (218 reputation)SSC Veteran (218 reputation)SSC Veteran (218 reputation)SSC Veteran (218 reputation)

Group: General Forum Members
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.

Tim


The 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 3
Conversione 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 datetime
select @dt = <an acceptable date format for January 3, 2012>
select dateadd(mm,2,@dt) as two_months_later

Tim
SQLRNNR
SQLRNNR
SSC-Insane
SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)

Group: General Forum Members
Points: 21075 Visits: 18259
Thanks for the question.



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

John Esraelo-498130
John Esraelo-498130
Mr or Mrs. 500
Mr or Mrs. 500 (547 reputation)Mr or Mrs. 500 (547 reputation)Mr or Mrs. 500 (547 reputation)Mr or Mrs. 500 (547 reputation)Mr or Mrs. 500 (547 reputation)Mr or Mrs. 500 (547 reputation)Mr or Mrs. 500 (547 reputation)Mr or Mrs. 500 (547 reputation)

Group: General Forum Members
Points: 547 Visits: 1029
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
John Esraelo-498130
Mr or Mrs. 500
Mr or Mrs. 500 (547 reputation)Mr or Mrs. 500 (547 reputation)Mr or Mrs. 500 (547 reputation)Mr or Mrs. 500 (547 reputation)Mr or Mrs. 500 (547 reputation)Mr or Mrs. 500 (547 reputation)Mr or Mrs. 500 (547 reputation)Mr or Mrs. 500 (547 reputation)

Group: General Forum Members
Points: 547 Visits: 1029
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
Go


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

































































































































































SQLServerCentral


Search