Tuesday, 6 December 2016

SQL Function Get Total Time in HH:MM:SS

  1. CREATE FUNCTION [dbo].TotalTime  
  2. (                      
  3.   @StartTime DATETIME,                      
  4.   @EndTime DATETIME  
  5. )                      
  6. RETURNS Varchar(10)                      
  7. AS                      
  8. begin     
  9. DECLARE @D VARCHAR(400)='31784'  
  10. Declare @outTime Varchar(10);      
  11. SELECT @D = DATEDIFF(SECOND,@StartTime,@EndTime)   
  12.   
  13.   
  14.  SELECT @outTime =CONVERT(VARCHAR(5), @D/60/60)  
  15.   + ':' + RIGHT('0' + CONVERT(VARCHAR(2), @D/60%60), 2)  
  16.   + ':' + RIGHT('0' + CONVERT(VARCHAR(2), @D % 60), 2)  
  17.   
  18.                      
  19.  RETURN  @outTime                      
  20. END    
  21. GO  
  22.   
  23.   
  24.   
  25.   
  26.    SELECT dbo.TotalTime('2016-08-10 10:36:01.000','2016-08-10 19:25:45.000'

No comments:

Post a Comment

STEPS TO SAVE YOUR EYES WHILE WORKING ON COMPUTER

STEPS TO SAVE YOUR EYES WHILE WORKING ON COMPUTER This is the report for all you people who works more and more on computer for long t...