If the difference is greater than a certain amount (a little over 68 years), you will get an overflow on DATEDIFF(DD,StartDate,EndDate) and will need a more complex seconds difference calculation:print 'Diff 1'goselect DD1=datediff(ss,'19500101','20180119 03:14:07')goprint 'Diff 2'goselect DD2=datediff(ss,'19500101','20180119 03:14:08')goprint 'Diff in Seconds'select a.*, SecondsDiff = -- Days difference time 86400 (number of seconds in a day) (datediff(dd,a.DT1,a.DT2)*000000000086400)+ -- Seconds since start of day datediff(ss,dateadd(dd,datediff(dd,a.DT1,a.DT2),a.DT1),a.DT2)from ( -- Test Data select DT1 = convert(datetime,'19000101'), DT2 = getdate() union all select DT1 = '19500101', DT2 = '20180119 03:14:07' union all select DT1 = '19500101', DT2 = '20180119 03:14:08' union all select DT1 = '17530101', DT2 = '99990101 23:59:59' ) a
Results:Diff 1DD1-----------2147483647(1 row(s) affected)Diff 2Msg 535, Level 16, State 0, Line 1The 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.Diff in SecondsDT1 DT2 SecondsDiff----------------------- ----------------------- ---------------------------------------1900-01-01 00:00:00.000 2012-05-29 09:22:07.713 35472721271950-01-01 00:00:00.000 2018-01-19 03:14:07.000 21474836471950-01-01 00:00:00.000 2018-01-19 03:14:08.000 21474836481753-01-01 00:00:00.000 9999-01-01 23:59:59.000 260218655999(4 row(s) affected)
CODO ERGO SUM