0

I am doing the function of booking a hotel, when I get the rooms available by date by following

SELECT * 
FROM oder 
WHERE
    startDate >= '2020-12-26 06:53:18.114+00' AND 
    endDate <= '2020-12-30 06:53:18.114+00'

but it works when i enter the exact start and end date stored in the database, i want when i enter the start and end date time can be within the period stored in the database, I can also get the room that was booked.

example: I choose the startDate = 2020-12-27 and the endDate=2020-12-28is within the period of 2020-12-26 to 2020-12-30, I want the result to return like the above query

SELECT * 
FROM oder 
WHERE
    startDate >= '2020-12-26 06:53:18.114+00' AND
    endDate <= '2020-12-28 06:53:18.114+00'
1
  • Please add some sample data and the expected output. Incl. some data which wouldn't fit the condition. Commented Jan 7, 2021 at 13:57

2 Answers 2

1

Looks like you might be interested in a range datatype, tsrange in this case. When using a range, you can also create a constraint to avoid double bookings.

The various range functions will help you to find the records you're looking for.

edit: When using a range and looking for records that overlap, this query might do the job:

SELECT
    * 
FROM
    oder 
WHERE
    tsrange_field && tsrange ( '2020-12-26 06:53:18.114+00', '2020-12-28 06:53:18.114+00', '[]' );

Assumption: Field tsrange_field will be the field where you store your timestamp ranges.

Sign up to request clarification or add additional context in comments.

Comments

0

If I followed you correctly, You need the rooms booked for the period that is given by you as an input.

You can use the OR condition and BETWEEN as follows:

SELECT * 
  FROM oder 
 WHERE startDate between '2020-12-26 06:53:18.114+00' AND '2020-12-30 06:53:18.114+00'
    or endDate between '2020-12-26 06:53:18.114+00' AND '2020-12-30 06:53:18.114+00'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.