Results 1 to 10 of 10

Thread: Compute datetime field addition

Hybrid View

  1. #1
    Join Date
    May 2010
    Posts
    9
    I used the below block to get the AverageTime field by adding the 2 date columns by first converting them to seconds and then dividing it with a number field.

    Code:
    begin
    
    declare @date1 nvarchar(100), @date2 nvarchar(100), @FinalDate nvarchar(100), @count int
    
    create table #temp6 (tks int, date1 nvarchar(100), date2 nvarchar(100), FinalDate nvarchar(100))
    
    select @count = count(eventcode) from trackedplusimport_temp where deviceid=13768
    SELECT @date1 = Convert(nvarchar(10),sum(DatePart(hh,ScanDateTime)))+':'+ Convert(nvarchar(10),sum(DatePart(mi,ScanDateTime)))+':'+ Convert(nvarchar(10),sum(DatePart(ss,ScanDateTime))) FROM trackedplusimport_temp WHERE deviceid=13768
    SELECT @date2 = Convert(nvarchar(10),sum(DatePart(hh,DataDateTime)))+':'+ Convert(nvarchar(10),sum(DatePart(mi,DataDateTime)))+':'+ Convert(nvarchar(10),sum(DatePart(ss,DataDateTime))) FROM trackedplusimport_temp WHERE deviceid=13768
    
    SELECT @FinalDate =
    convert(nvarchar(10),
    (
     (
    	(DATEPART(hour, convert(nvarchar,@date1,108)) * 60 * 60) +
    	(DATEPART(minute,convert(nvarchar,@date1,108)) * 60) +
    	 DATEPART(second, convert(nvarchar,@date1,108))
     )
     +
     (
    	(DATEPART(hour, convert(nvarchar,@date2,108)) * 60 * 60) +
    	(DATEPART(minute, convert(nvarchar,@date2,108)) * 60) +
    	 DATEPART(second, convert(nvarchar,@date2,108))
     )
    )
    )
    --select @c = @b/@count
    
    insert into #temp6 values(@count, @date1, @date2, @FinalDate)
    
    end
    
    --drop table #temp6
    --select * from #temp6
    When Executing the above, Im getting the below error..


    Arithmetic overflow error converting expression to data type datetime


    How can I overcome this error?

    Thanks in advance

  2. #2
    Join Date
    May 2010
    Posts
    9
    Just some more info..

    When Date1 and Date2 are upto : '23:59:59', the query is working fine. But when it becomes '24:00:00' - I am getting Overflow error.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •