terrible-coder
terrible-coder

Reputation: 333

SQL Server Query for selecting rows with times based on a specified user-inputted timespan

Is there a way to query a database to return rows containing data with a time datatype that is dependent on the user providing a time span (like for example, querying between 5:00 to 11:00 will return all rows containing times from 5:00 all the way to 11:00 and not simply exactly 5:00 and 11:00)?

Is the problem stated above possible? Or I should only rely on programatically setting the parameters in my program?

I know that this one will definitely not work with what I aim to know.

SELECT *   
FROM Consultation
WHERE (Consultation_Day = 'Monday') AND (Time_From = '5:00') OR (Time_From = '11:00')
ORDER BY Last_Name

Upvotes: 1

Views: 76

Answers (2)

Hedinn
Hedinn

Reputation: 864

Or since you are using MS SQL Server you can use between

where Time_From BETWEEN '5:00' and '11:00'

So your query would be

SELECT *   
FROM Consultation
WHERE Consultation_Day = 'Monday' AND Time_From BETWEEN '5:00' AND '11:00'
ORDER BY Last_Name

Upvotes: 0

Habib
Habib

Reputation: 223312

Your condition for Time is checking for exact time of either 5:00 or 11:00 for range you can do:

(Time_From >= '5:00') AND (Time_From <= '11:00')

So your query should be:

SELECT *   
FROM Consultation
WHERE (Consultation_Day = 'Monday') AND ((Time_From >= '5:00') AND (Time_From <= '11:00'))
ORDER BY Last_Name

Upvotes: 2

Related Questions