473,472 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to do compare ranges of dates in query

1 New Member
hello...
I have a program that saves dates.

Problem:
Given:

sample table: Leave
------------------------------------------
ID | UserID | DateFrom | DateTo|
----------------------------------------------------
1 | 060807| 2006-01-06 | 2006-01-15
2 | 060807| 2006-01-16 | 2006-01-20
----------------------------------------------------

Input from the user:

Example A:
ID : 3
UserID : 060807
DateFrom: 2006-01-13
DateTo: 2006-01-14

Query Result: Cannot add: Range of dates already exist.

Example B:
ID : 3
UserID : 060807
DateFrom: 2006-01-21
DateTo: 2006-01-23

Query Result: Data added succesfully.


Question:
what mysql query can I use to check the range of the DateFrom and DateTo from the user and compare it to the table and check whether it would conflict to the existing ranges of dates?

Thank you very much and I hope anyone can help me....huhuhu
Jan 17 '07 #1
2 2725
radcaesar
759 Recognized Expert Contributor
Use datediff() method

any problems, refer Date and Time functions
:)

hello...
I have a program that saves dates.

Problem:
Given:

sample table: Leave
------------------------------------------
ID | UserID | DateFrom | DateTo|
----------------------------------------------------
1 | 060807| 2006-01-06 | 2006-01-15
2 | 060807| 2006-01-16 | 2006-01-20
----------------------------------------------------

Input from the user:

Example A:
ID : 3
UserID : 060807
DateFrom: 2006-01-13
DateTo: 2006-01-14

Query Result: Cannot add: Range of dates already exist.

Example B:
ID : 3
UserID : 060807
DateFrom: 2006-01-21
DateTo: 2006-01-23

Query Result: Data added succesfully.


Question:
what mysql query can I use to check the range of the DateFrom and DateTo from the user and compare it to the table and check whether it would conflict to the existing ranges of dates?

Thank you very much and I hope anyone can help me....huhuhu
Jan 17 '07 #2
ronverdonk
4,258 Recognized Expert Specialist
Given: when the user-requested start date or end date falls within the date ranges in any of the user's records, it means that any or both of these dates are reserved already.

See the following statements that do this for your samples (I use your thread's dates to make it more visible).
Expand|Select|Wrap|Line Numbers
  1. mysql> SELECT * FROM a WHERE uid=060807 
  2.        AND '2006-01-13'  BETWEEN date_from AND date_to 
  3.        OR  '2006-01-14'  BETWEEN date_from AND date_to;
  4. +----+-------+------------+------------+
  5. | id | uid   | date_from  | date_to    |
  6. +----+-------+------------+------------+
  7. |  1 | 60807 | 2006-01-06 | 2006-01-15 |
  8. +----+-------+------------+------------+
  9. 1 row in set (0.00 sec)
  10. mysql> SELECT * FROM a WHERE uid=060807 
  11.        AND '2006-01-21' BETWEEN date_from AND date_to 
  12.        OR  '2006-01-23' BETWEEN date_from AND date_to;
  13. Empty set (0.00 sec)
  14. mysql>
So when something is returned, the dates are taken.
When an empty result is returned you can book the user.

Ronald :cool:
Jan 17 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Gleep | last post by:
Hey Guys, I've got a table called Outcomes. With 3 columns and 15 rows 1st col 2nd col 3rdcol outcome date price There are 15 rows...
3
by: Myron | last post by:
I'm trying to create a query that will tell me which requests took longer than 10 days to move one from particular state to another state. The query I've created returns the correct requests, but...
9
by: Thomas R. Hummel | last post by:
Hello, I am importing data that lists rates for particular coverages for a particular period of time. Unfortunately, the data source isn't very clean. I've come up with some rules that I think...
12
by: Steve Elliott | last post by:
I have a query set up to gather together data between two specified dates. Shown in the query column as: Between #24/09/2004# And #01/10/2004# Is it possible to enter several different date...
67
by: PC Datasheet | last post by:
Transaction data is given with date ranges: Beginning End 4/1/06 4/4/06 4/7/06 4/11/06 4/14/06 4/17/06 4/18/06 4/21/06 426/06 ...
7
by: No bother | last post by:
I have a table which has, among other fields, a date field. I want to get a count of records where certain criteria are met for, say, three days in a row. For example: NumWidgets Date...
3
by: prime80 | last post by:
I have a database report that is based on a query that returns actions taken on certain dates. The data looks like this: (The report based on this query is layed out similarly) Date ...
9
by: mharrison | last post by:
Hello, I am developing a small java web-based car-pool booking system app which interacts with an access database. I am trying to write 2 queries: The first which will specify whether a given car...
1
by: Matt | last post by:
Hi all, I have a database with a table storing a list of names, invoice dates, and invoice amounts. What I'm looking to do is to create a sum of the invouice amounts based on a range of...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.