You definitely want to reverse engineer the database. Putting multiple values ββin one column like this is a huge violation of normalization rules and will cause many headaches in the future. One column should always contain separate information.
You must also use the correct data types. Do not put time in a string because you can end up with βfooβ as time, and then what do you do?
Instead, you probably want to:
CREATE TABLE Restaurants ( restaurant_id INT NOT NULL, restaurant_name VARCHAR(40) NOT NULL, CONSTRAINT PK_Restaurants PRIMARY KEY CLUSTERED (restaurant_id) ) CREATE TABLE Restaurant_Hours ( restaurant_id INT NOT NULL, hours_id INT NOT NULL, day_of_week SMALLINT NOT NULL, start_time TIME NOT NULL,
Then you can easily check open restaurants at specific times:
SELECT R.restaurant_id, R.restaurant_name FROM Restaurants R WHERE EXISTS ( SELECT * FROM Restaurant_Hours RH WHERE RH.restaurant_id = R.restaurant_id AND RH.start_time <= @time AND RH.end_time >= @time AND RH.day_of_week = @day_of_week )
If you have a time interval that spans midnight, you will need to have two rows β one on the first day and one at midnight β βxβ the next day. Also, be sure to consider time zones when using the GUI.
Tom h
source share