Mohammed Ali Asgar
Mohammed Ali Asgar

Reputation: 47

SQL Server compare time in IF Condition

I am trying to compare starttime and GETDATE() time by taking there time part but in SQL Server if condition is not satisfying even though it should do by seeing .

IF (CONVERT(VARCHAR(10), GETDATE(), 108) <= CONVERT(VARCHAR(10),(DATEADD(minute, 5, GETDATE())), 108))
BEGIN
    -- Do something 
END
ELSE
BEGIN
    -- Do Next thing
END

Should I convert the time to other format before comparing and using in IF condition ? Please help

Upvotes: 0

Views: 1198

Answers (1)

GMB
GMB

Reputation: 222392

I you want to compare the time portions, you can cast() your values as time, like:

cast(getdate() as time) <= cast(dateadd(minute, 5, starttime) as time)

The reason why you want to do this is rather unclear; beware that, with this techinque, unexpected things may happen when starttime has a time portion that is greater than 23:55.

Upvotes: 2

Related Questions