问题
I am using sql server 2008 R2 and due to one problem i am able to know that smalldatetime round ss to nearest minute . here is content from MSDN .
ss is two digits, ranging from 00 to 59, that represent the second. Values that are 29.998 seconds or less are rounded down to the nearest minute, Values of 29.999 seconds or more are rounded up to the nearest minute.
Now i face one problem due to this, that i have parameter type in my storedproceduer is smalldatetime when i pass '2014-03-23 23:59:59' its converted to date 2014-03-24 00:00:00
I found the solution that i should convert parameters from smalldatetime to nvarchar(30)
and problem solved.
but my real question why such behavior of smalldatetime rounding of ss that change the day ?
you can try it with following queries
DECLARE @EndDate smallDatetime
SET @EndDate = '2014-03-23 23:59:59'
SELECT @EndDate
DECLARE @EndDate smallDatetime
SET @EndDate = '2014-03-23 23:59:30'
SELECT @EndDate
DECLARE @EndDate smallDatetime
SET @EndDate = '2014-03-23 23:59:29'
SELECT @EndDate
回答1:
Alarm bells go off in my head any time I see someone using 23:59:59 in SQL.
This is almost always because they want to return a full days worth of data in the query by doing something like:
WHERE date_field BETWEEN '2014-03-19 00:00:00' AND '2014-03-19 23:59:59'
This is because BETWEEN includes the boundary values and you might not want values at 2014-03-20 00:00:00 included.
The above query is equivalent to:
WHERE date_field >= '2014-03-19 00:00:00'
AND date_field <= '2014-03-19 23:59:59'
The Correct Approach
WHERE date_field >= '2014-03-19 00:00:00'
AND date_field < '2014-03-20 00:00:00'
Notice the subtle difference in the operators.
回答2:
Because that is the nearest minute.
Given it is defined as rounding up at 29.998 seconds or above, what other result would you expect? At 23:58:30, you expect it to round to 23:59 - why should the behaviour be any different a minute later?
If you want a more precise date/time measurement, use datetime, or datetime2 - not nvarchar
来源:https://stackoverflow.com/questions/22500563/why-ss-round-to-nearest-minute-that-will-change-day