Function To Convert Seconds to Hour: Minute: Seconds Format in Sql Server

Open sql server and create following function

  1. CREATE FUNCTION [dbo].[Fn_SecondsConversion]  
  2.  (  
  3. @TimeinSec VARCHAR(MAX)  
  4. )  
  5. RETURNS VARCHAR(MAX)  
  6. BEGIN  
  7. DECLARE @op VARCHAR(MAX)  
  8. SET @op = ''  
  9. SELECT @op=RIGHT('0' + CAST(@TimeinSec / 3600 AS VARCHAR),2) + ':' +  
  10. RIGHT('0' + CAST((@TimeinSec / 60) % 60 AS VARCHAR),2) + ':' +  
  11. RIGHT('0' + CAST(@TimeinSec % 60 AS VARCHAR),2) 
    RETURN @op  
  12. End 

This function will accept seconds as parameter and return the Hours: Minutes: Seconds conversion of total second.

Use above function like

  1. SELECT [dbo].[Fn_SecondsConversion]('30000'

Following output u will get when you run that select query