How to make an hour comparison in sql

Asked

Viewed 1,506 times

3

I have a sql query :

select 
 HORA_FECHAMENTO, --campo char no recebendo a hora no formato 18:00     
 CONVERT(VARCHAR(11),GETDATE(),114) as HORA_ATUAL
 from TB_ESTRACAO where IDEXTRACAO = 4 

I want to compare the server time, it always needs to be higher than the HORA_FECHAMENTO time

1 answer

2


Although you have not specified the server version, go a solution to 2008 version or higher.

Declare @HORA_FECHAMENTO char (5)
Declare @HORA_ATUAL datetime
Set @HORA_FECHAMENTO='04:59'
Set @HORA_ATUAL = CAST(CONVERT(VARCHAR(11),GETDATE(),8) as time)

select 
 CAST(@HORA_FECHAMENTO AS time) as HORA_FECHAMENTO, --campo char no recebendo a hora no formato 18:00     
 CAST(CONVERT(VARCHAR(11),GETDATE(),8) as time) as HORA_ATUAL

IF @HORA_ATUAL > @HORA_FECHAMENTO
BEGIN
    print 'Hora servidor maior'
END

HORA_FECHAMENTO | HORA_ATUAL
--------------------------------| ----------------
04:59:00.0000000 | 04:59:11.0000000

(1 Row(s) affected)

Server time

  • 1

    Right answer, immensely grateful!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.