Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
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: 198 | Views in the last 30 days: 2
 
Related Articles
FORUM

Different Behaviour of VARCHAR and NVARCHAR

Variables declared with VARCHAR and NVARCHAR were behaviouring differently.

FORUM

varchar issue

varchar issue

FORUM

Tricky ...VARCHAR

VARCHAR logics

FORUM

Variable Declaration

Variable Declaration

FORUM

Must declare the variable '@ComputerName'.

Must declare the variable '@ComputerName'.

 
Contribute