How to do a time comparison in sql sever

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 must always be greater than the hour HOUR_DATE

    
asked by anonymous 10.01.2016 / 03:03

1 answer

2

Although you have not specified the server version, there is a solution for version 2008 or later.

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
  

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

     

(1 row (s) affected)

     

Larger server time

    
10.01.2016 / 14:03