Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to use the time portion of a datetime column
Message
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Miscellaneous
Thread ID:
00557574
Message ID:
00557581
Views:
22
This message has been marked as the solution to the initial question of the thread.
>I'd like to find all rows where the time portion of a date_time column is between 11:00AM and 1:15PM.
>
>I seem to get into quite a rat's nest of convert(), datediff() and dateadd() combinations when looking for a simple way to do this.
>
>Thanks,
>Mike

Here's test code and select statement at the end.
DECLARE @dt datetime, @hhmm char(5), @t_min char(5), @t_max char(5)

SET @dt = GETDATE()
SET @hhmm = CONVERT ( char(5), @dt, 8)

SET @t_min = '11:00'
SET @t_max = '13:15'

PRINT @dt
PRINT @hhmm

IF @hhmm >= @t_min AND @hhmm <= @t_max
	PRINT 'Time is between ' + @t_min + ' and ' + @t_max
ELSE
	PRINT 'Time is out of range'

* SQL Select statement
SELECT * FROM mytable ;
    WHERE CONVERT ( char(5), mydatefield, 8) BETWEEN  @t_min AND @t_max
--sb--
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform