0

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!

jarlh
  • 42,561
  • 8
  • 45
  • 63
Martin
  • 55
  • 1
  • 8

1 Answers1

1

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
Jamie Pollard
  • 1,571
  • 1
  • 10
  • 21