Martin February 2016

SQL Server see which time is greater

I need to pull the time from a field in an existing table which is stored as SMALLDATETIME.

An example of the value stored is as follows:

Jun  4 1900 11:30AM

I need to grab the time part (hours and minutes) and see if it is less than whatever the current time is now.

EG IF @CutoffTime < @CurrentTime THEN
BEGIN
....
END

Is there an easy way to do this in SQL Server? Thanks!

Answers


Jamie Pollard February 2016

You can cast the datetime to a time, along with the time from getutcdate(), and compare:

DECLARE @timeA SMALLDATETIME;

SET @timeA = 'Jun  4 1900 11:30AM'

IF CAST(@timeA AS TIME) < CAST(GETUTCDATE() AS TIME)
BEGIN

  PRINT 'Time earlier than current time'

END

Post Status

Asked in February 2016
Viewed 2,486 times
Voted 7
Answered 1 times

Search




Leave an answer