SQL Server: datediff function resulted in an overflow when using MILLISECOND

o4hqfura  于 2023-03-07  发布在  SQL Server
关注(0)|答案(8)|浏览(147)

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.

mlnl4t2r

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

3htmauhk

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.

70gysomp

70gysomp3#

In SQL Server 2016 there is a new function available: DATEDIFF_BIG

It solves exactly the overflow problem.

d5vmydt9

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:

SELECT CONVERT(varchar(12), 
        CAST('2014-11-04 08:21:17.723' as datetime) - 
        CAST('2014-08-04 10:37:28.713' as datetime)
       , 114)
wwtsj6pe

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 .

nbnkbykc

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.

SELECT DATEDIFF_BIG( 
    millisecond, 
    SYSDATETIME(), 
    DATEADD(year, 1000, SYSDATETIME()) ) AS 'Milliseconds in 1000 years';
ih99xse1

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.

declare @x as datetime = getdate()

-- epoch_delta_s_to_date * 1000 + day_delta_ms
select
cast(datediff(second, '1970-01-01', cast(@x as date)) as bigint) * 1000
  + cast(datediff(millisecond, cast(@x as date), @x) as bigint)

For the case of obtaining a "JavaScript Time Epoch" this is still subject to the Y2038 limitation of datediff(second, '1970-01-01', ..) .

41ik7eoe

41ik7eoe8#

I have to use DATEDIFF_BIG in SQL versions before SQL2016 and have written my own function:

Declare @datestart datetime = '1953-01-01 23:18:09.284'
Declare @dateend datetime = '9999-12-31 23:54:03.844'

select 
/*DATEDIFF_BIG(millisecond, @datestart, @dateend) AS [ForTestComparion], */
(cast(DATEDIFF(DAY, @datestart, @dateend) as bigint) * 24 * 60 * 60 * 1000 )
+ 
    DATEDIFF(millisecond, 
    DATETIMEFROMPARTS(2000, 1, 1,DATEPART(HOUR, @datestart), DATEPART(MINUTE, @datestart), DATEPART(SECOND, @datestart), DATEPART(MILLISECOND, @datestart)),
    DATETIMEFROMPARTS(2000, 1, 1,DATEPART(HOUR, @dateend), DATEPART(MINUTE, @dateend), DATEPART(SECOND, @dateend), DATEPART(MILLISECOND, @dateend)))

2000, 1, 1 can be any date just need to be same day to compare only hours,minute,second,milisecond

相关问题