sql-server – How to do a time comparison in sql sever

Question:

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 greater than the HORA_CLOSING time

Answer:

Although the server version is not specified, a solution will be version 2008 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

TIME_CLOSING | CURRENT TIME
——————————–| —————-
04:59:00.0000000 | 04:59:11.0000000

(1 row(s) affected)

higher server time

Scroll to Top