May 8, 2012 at 5:59 am
Can you briefly explain the working of below query
select DATEDIFF(MONTH,0,getdate())
In my machine it returned as 1348
May 8, 2012 at 6:10 am
it is substituting 0 with the default date of '1900-01-01 00:00:00.000'
If only a time value is assigned to a variable of a date data type, the value of the missing date part is set to the default value: 1900-01-01. If only a date value is assigned to a variable of a time or date data type, the value of the missing time part is set to the default value: 00:00:00. If either startdate or enddate have only a time part and the other only a date part, the missing time and date parts are set to the default values.
As your not passing in a date or time for the starting datetime it is using its own default based on the MSDN quote above.
May 8, 2012 at 7:23 am
anthony.green (5/8/2012)
it is substituting 0 with the default date of '1900-01-01 00:00:00.000'
You can change the "zero" date at a server level, so test it before you count on it being "1900-01-01 00:00:00.000". Obviously this doesn't matter if it's being used for stripping parts off a datetime (e.g. get 1st of month etc), but may matter if it's being used in other cases.
May 8, 2012 at 7:31 am
Cadavre (5/8/2012)
You can change the "zero" date at a server level
Can you?
May 8, 2012 at 7:32 am
Cadavre (5/8/2012)
anthony.green (5/8/2012)
it is substituting 0 with the default date of '1900-01-01 00:00:00.000'You can change the "zero" date at a server level, so test it before you count on it being "1900-01-01 00:00:00.000". Obviously this doesn't matter if it's being used for stripping parts off a datetime (e.g. get 1st of month etc), but may matter if it's being used in other cases.
Would you please elaborate on this? I was not aware that you could change the '0' date.
May 8, 2012 at 8:14 am
I'm sure I've read it somewhere, but to be honest after a quick search all I can find is this: -
You can actually change the zero date at a server-level, so test it before you count on that. Won't matter if it's being used to strip parts off of dates (like midnight calculations or 1st of month calculations), but might matter if it's being used for something else.[/url]
So I may have to strike out my previous comment.
May 8, 2012 at 8:26 am
Cadavre (5/8/2012)
I'm sure I've read it somewhere, but to be honest after a quick search all I can find is this: -
You can actually change the zero date at a server-level, so test it before you count on that. Won't matter if it's being used to strip parts off of dates (like midnight calculations or 1st of month calculations), but might matter if it's being used for something else.[/url]So I may have to strike out my previous comment.
Then we must get GSquared to elaborate on his comment! 😀
May 8, 2012 at 11:17 am
anthony.green (5/8/2012)
it is substituting 0 with the default date of '1900-01-01 00:00:00.000'If only a time value is assigned to a variable of a date data type, the value of the missing date part is set to the default value: 1900-01-01. If only a date value is assigned to a variable of a time or date data type, the value of the missing time part is set to the default value: 00:00:00. If either startdate or enddate have only a time part and the other only a date part, the missing time and date parts are set to the default values.
As your not passing in a date or time for the starting datetime it is using its own default based on the MSDN quote above.
Actually, that's not true. You are passing in a date expressed as an integer. You can see this by running the following:
SELECT DATEDIFF(DAY, 7, '1900-01-08')
If what you said were true, it would substitute the default date and time for the 7 and produce a result of 7, whereas the actual result is 0. It's debatable whether you are also specifying a time, although clearly 0.0 would also specify a time of 00:00:00.
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
Viewing 8 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply