Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Greater than, Equal to AND Less Than

Tags:

sql

select

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.

like image 710
user1081326 Avatar asked Mar 07 '12 22:03

user1081326


People also ask

How do you write less than or equal to in SQL query?

<= (Less Than or Equal To) (Transact-SQL)

Is it better to use <> or != In SQL?

Here is the answer – You can use either != or <> both in your queries as both technically same but I prefer to use <> as that is SQL-92 standard.

Can you use <= in SQL?

While some databases like sql-server support not less than and not greater than, they do not support the analogous not-less-than-or-equal-to operator ! <=.


2 Answers

Supposing you use sql server:

WHERE StartTime BETWEEN DATEADD(HOUR, -1, GetDate())
                    AND DATEADD(HOUR, 1, GetDate())
like image 63
zerkms Avatar answered Sep 27 '22 21:09

zerkms


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.

like image 43
James Hay Avatar answered Sep 27 '22 20:09

James Hay