[sql] SQL Greater than, Equal to AND Less Than

I want to create a query like the following, But im unsure of how to code it correctly, I want it to return all bookings within 1 hour of a StartTime, Here is what i came up with:

SELECT BookingId, StartTime
FROM Booking
WHERE StartTime <=> 1.00

Is the possible? or Is there a way round it?

Everything ive found on the web hasn't been about using Greater than, Equal to and Less Than all in the same query.

This question is related to sql select

The answer is


declare @starttime datetime = '2012-03-07 22:58:00'

SELECT BookingId, StartTime
FROM Booking
WHERE ABS( DATEDIFF( minute, StartTime, @starttime ) ) <= 60

Supposing you use sql server:

WHERE StartTime BETWEEN DATEADD(HOUR, -1, GetDate())
                    AND DATEADD(HOUR, 1, GetDate())

If start time is a datetime type then you can use something like

SELECT BookingId, StartTime
FROM Booking
WHERE StartTime >= '2012-03-08 00:00:00.000' 
AND StartTime <= '2012-03-08 01:00:00.000'

Obviously you would want to use your own values for the times but this should give you everything in that 1 hour period inclusive of both the upper and lower limit.

You can use the GETDATE() function to get todays current date.


Somthing like this should workL

SELECT BookingId, StartTime
FROM Booking
WHERE StartTime between dateadd(hour, -1, getdate()) and getdate()