I have the following query :
select CONVERT(varchar(12), DATEADD(MILLISECOND, DateDiff(MILLISECOND, '2014-08-04 10:37:28.713','2014-11-04 08:21:17.723'), 0), 114)
When I execute this, I get the error : "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."
When I change the query to the following it works fine :
select CONVERT(varchar(12), DATEADD(SECOND, DateDiff(SECOND, '2014-08-04 10:37:28.713','2014-11-04 08:21:17.723'), 0), 114)
The problem is that I really need the MILLISECONDS as well.
8条答案
按热度按时间mlnl4t2r1#
A bit later response but may help. In SQL 2016 MS introduced function DATEDIFF_BIG which will (according to type size) overflow in difference bigger than something like 290k years. But technet article have same time difference as basic DATEDIFF - https://msdn.microsoft.com/en-us/library/mt628058.aspx
3htmauhk2#
See https://learn.microsoft.com/en-us/sql/t-sql/functions/datediff-transact-sql?view=sql-server-ver15#return-value
For millisecond, the maximum difference between startdate and enddate is 24 days, 20 hours, 31 minutes and 23.647 seconds.
If you need millisecond above that level, you'll need to write something custom.
70gysomp3#
In SQL Server 2016 there is a new function available: DATEDIFF_BIG
It solves exactly the overflow problem.
d5vmydt94#
You don't need to refer to the miliseconds in your calculation.
This will do exactly the same as your script except the overflow:
wwtsj6pe5#
For me there was a big interval between two dates so i have used below code
declare @timetagInMillsecond bigint=CAST(CAST( cast(@timetag as datetime) -'1970-01-01' AS decimal(38,10))2460601000+0.5 as bigint)
It works for me .
nbnkbykc6#
Use DATEDIFF_BIG to resolve the overflow issue
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.
ih99xse17#
For SQL Server 2014, the following works around the 'int' limitation to obtain a "JavaScript Time Epoch". This assumes the start epoch is itself a date, which fits the local use-case leading to finding this question. The query requires adaptation to the specific question use-case which does not have this property.
For the case of obtaining a "JavaScript Time Epoch" this is still subject to the Y2038 limitation of
datediff(second, '1970-01-01', ..)
.41ik7eoe8#
I have to use DATEDIFF_BIG in SQL versions before SQL2016 and have written my own function:
2000, 1, 1 can be any date just need to be same day to compare only hours,minute,second,milisecond