What does this error mean and how can I avoid it?
The datediff function resulted in an overflow. The number of dateparts separating two date/time instances is too large. Try to use datediff with a less precise datepart.
I am not using the datediff function. I am doing this query where Timestamp is a datetime type:
SELECT TOP 10 * from vSomeView
WHERE TimestampUTC >= '2009-08-13 22:17:00'
What could I be doing wrong?
I'm using SQL Server 2008.
5条答案
按热度按时间bbmckpt71#
SQL Server may be doing a DATEDIFF internally for the comparison, and if the two dates are much more than 68 years apart (and the internal DATEDIFF is by seconds), DATEDIFF can error as the output of DATEDIFF is an INT.
I've bumped into this before (using DATEDIFF directly) and resolved it by casting DATETIMEs to DECIMALs as follows:
2lpgd9682#
I had the same issue because one of the records in my table had a default value for a datetime field of 1900-01-01 00:00:00.000.
DATEDIFF in the where clause will be evaluated for all the records in the table and will overflow on the LastCheckIn with value 1900-01-01 00:00:00.000
I solved it by first evaluating DATEDIFF for a difference in YEARS < 1
This is the final query:
u5rb5r593#
Thank you all for the pointers!
They made me recheck the vSomeView and it turns out that the vSomeView was doing a join between a view and some other tables. That view was doing a datediff to convert some datetime into a posix-style timestamp (seconds since epoch). Once I removed it, the query runs fine.
vom3gejh4#
SQL Server 2016 added
DATEDIFF_BIG()
which returnsbigint
.kyvafyod5#
I created function which should bypass this limitation on SQL versions lower than SQL2016 see here .
It should work similliar to DATETIMEDIFF_BIG in simple scenarios