Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Converte Inteiro em Horas

By wamj,

/******************************************************************************
** Função para converter valores inteiros em formato de hora.
** A variável @TEMPO recebe os valores inteiros em segundos e formata para o padrão de hh:mm:ss
**
** Criado por: Wilson junior - 27/05/2011
** Email: wilsonamjunior@gmail.com
******************************************************************************
*/

CREATE FUNCTION FN_HORA (@TEMPO INT)

RETURNS VARCHAR(20)
AS
BEGIN
DECLARE @HORARIO VARCHAR(20)

DECLARE @HORA    INT
DECLARE @MINUTO  INT
DECLARE @SEGUNDO INT

SET @HORA    = (@TEMPO/3600)
SET @MINUTO  = (@TEMPO%3600) / 60
SET @SEGUNDO = (@TEMPO%3600) % 60


SELECT @HORARIO =
    CASE WHEN @TEMPO/3600 >= 1 THEN

        CASE LEN(CAST((@HORA)        AS VARCHAR)) WHEN 1 THEN '0' ELSE '' END
      + CAST ((@HORA) AS VARCHAR) + ':'

      + CASE LEN(CAST((@MINUTO)        AS VARCHAR)) WHEN 1 THEN '0' ELSE '' END
      + CAST((@MINUTO) AS VARCHAR) +  ':'

      + CASE LEN(CAST((@SEGUNDO)    AS VARCHAR)) WHEN 1 THEN '0' ELSE '' END
      + CAST((@SEGUNDO) AS VARCHAR)

    ELSE
        CASE LEN(CAST((@MINUTO)        AS VARCHAR)) WHEN 1 THEN '0' ELSE '' END
      + CAST((@MINUTO) AS VARCHAR) +  ':'

      + CASE LEN(CAST((@SEGUNDO)    AS VARCHAR)) WHEN 1 THEN '0' ELSE '' END
      + CAST((@SEGUNDO) AS VARCHAR)
    END

RETURN(@HORARIO)
END

Total article views: 197 | Views in the last 30 days: 3
 
Related Articles
FORUM

Different Behaviour of VARCHAR and NVARCHAR

Variables declared with VARCHAR and NVARCHAR were behaviouring differently.

FORUM

varchar issue

varchar issue

FORUM

Variable Declaration

Variable Declaration

FORUM

Tricky ...VARCHAR

VARCHAR logics

FORUM

Must declare the variable '@ComputerName'.

Must declare the variable '@ComputerName'.

 
Contribute

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones
Editor, SQLServerCentral.com

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones