Get specific date from two dates in sql | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Get specific date from two dates in sql

I need to write a query to find out how many hotel bookings have been made during a specific month, let's say January. All rows have a to and from date, both in data type date. I've tried eg. month(dateto) and dateto between 'YYYY-MM-DD' AND 'YYYY-MM-DD', but if there was a booking from 2019-12-30 to 2020-02-01 my query won't find this. Any suggestion to how to solve this?

22nd Jan 2021, 5:47 PM
Jen
3 Answers
+ 1
Can you show any attempt from you?
22nd Jan 2021, 6:14 PM
JaScript
JaScript - avatar
0
Attempt 1: SELECT * FROM booking WHERE datefrom BETWEEN '2021-01-01' AND '2021-01-31' OR dateTo BETWEEN '2021-01-01' AND '2021-01-31'; Attempt 2: SELECT * FROM booking WHERE MONTH(dateFrom) = 1 OR MONTH(dateTo) = 1; but these wont find a booking with: dateFrom: "2020-12-31" dateTo: "2021-02-22"
22nd Jan 2021, 6:25 PM
Jen
0
Check this post about overlapping date ranges, it has detailed explanation of the solution. Also it is useful to add your SQL flavor/dialect in tags, because different database engines may have different approach to comparing dates. https://stackoverflow.com/questions/325933/determine-whether-two-date-ranges-overlap
23rd Jan 2021, 10:09 AM
Tibor Santa
Tibor Santa - avatar