Delphi Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreDelphi Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 10th, 2012, 01:12 AM
icuras icuras is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2007
Posts: 165 icuras User rank is Sergeant Major (2000 - 5000 Reputation Level)icuras User rank is Sergeant Major (2000 - 5000 Reputation Level)icuras User rank is Sergeant Major (2000 - 5000 Reputation Level)icuras User rank is Sergeant Major (2000 - 5000 Reputation Level)icuras User rank is Sergeant Major (2000 - 5000 Reputation Level)icuras User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Day 4 h 42 m 59 sec
Reputation Power: 36
Mysql between dates

How do i run a query for between dates?

to do ONE date for between is easy i know. but what if i have 2 dates?

i have a leave program. let say i take of from the 10th to the 15th. I want to run a query from the 7th to the 17th and then that record should appear.

It is just as if i am telling you that range 12-15 is between 10 and 20.

Can it be done?

Reply With Quote
  #2  
Old July 10th, 2012, 07:11 PM
clivew clivew is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jan 2006
Location: Carlsbad, CA
Posts: 2,045 clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 37 m
Reputation Power: 382
I am not sure I completely understand; but is this what you are looking for?
Pseudo SQL:

SQL Code:
Original - SQL Code
  1. SELECT * FROM TABLE T
  2. WHERE T.FROM_DATE >= 7th
  3. AND T.TO_DATE <= 17th


That will give you all records that fall within the range of interest.

If that is not what you want, could you try another explanation?

Reply With Quote
  #3  
Old July 11th, 2012, 10:35 AM
icuras icuras is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2007
Posts: 165 icuras User rank is Sergeant Major (2000 - 5000 Reputation Level)icuras User rank is Sergeant Major (2000 - 5000 Reputation Level)icuras User rank is Sergeant Major (2000 - 5000 Reputation Level)icuras User rank is Sergeant Major (2000 - 5000 Reputation Level)icuras User rank is Sergeant Major (2000 - 5000 Reputation Level)icuras User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Day 4 h 42 m 59 sec
Reputation Power: 36
re

Hi Clive

Lets take your example. Let say i take leave from the 2nd to the 15. Then that query of your would not pick me up. But i AM on leave in those periods.

It will fail on the WHERE clause the AND is fine. I would like to know who is on leave in between to periods.

You understand?Thanks again.

Reply With Quote
  #4  
Old July 11th, 2012, 10:58 AM
clivew clivew is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jan 2006
Location: Carlsbad, CA
Posts: 2,045 clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 37 m
Reputation Power: 382
OK. I understand better now.

Will this work?

SQL Code:
Original - SQL Code
  1. SELECT * FROM TABLE T
  2.     WHERE T.FROM_DATE <= 17th
  3.     AND T.TO_DATE >= 7th 

Reply With Quote
  #5  
Old July 11th, 2012, 11:14 PM
majlumbo majlumbo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2008
Posts: 255 majlumbo User rank is Lance Corporal (50 - 100 Reputation Level)majlumbo User rank is Lance Corporal (50 - 100 Reputation Level)majlumbo User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 3 Days 2 m 12 sec
Reputation Power: 5
I think what you want is to capture if either the from_date or the to_date are within your range,

select from_date, to_date from Table T
where
(T.From_Date between :startdate and :enddate) or
(T.To_Date between :startdate and :enddate)

use the query's parambyname property to set the startdate and enddate parameters.

This will include the record if either date is between the range, so if someone starts their leave on the end date, or if the leave ends on the start date, or if the leave is completely within the period, it will be selected.

Reply With Quote
  #6  
Old July 12th, 2012, 12:31 AM
clivew clivew is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jan 2006
Location: Carlsbad, CA
Posts: 2,045 clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 37 m
Reputation Power: 382
Quote:
Originally Posted by majlumbo
I think what you want is to capture if either the from_date or the to_date are within your range,

select from_date, to_date from Table T
where
(T.From_Date between :startdate and :enddate) or
(T.To_Date between :startdate and :enddate)


I agree that this looks correct.

My second suggestion should have used an OR not AND and would give the same
result I think; but this is a much clearer expression of the intent of the query
and therefore superior.

Clive

Reply With Quote
  #7  
Old July 12th, 2012, 02:38 PM
majlumbo majlumbo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2008
Posts: 255 majlumbo User rank is Lance Corporal (50 - 100 Reputation Level)majlumbo User rank is Lance Corporal (50 - 100 Reputation Level)majlumbo User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 3 Days 2 m 12 sec
Reputation Power: 5
Quote:
Originally Posted by clivew
I agree that this looks correct.

My second suggestion should have used an OR not AND and would give the same
result I think; but this is a much clearer expression of the intent of the query
and therefore superior.

Clive


Actually, my query does not solve the problem. If someone starts their leave before the start date, and ends it after the end date, then it wouldn't be selected even thought they were on leave during the period.

This should work however...

Code:
select from_date, to_date from Table T
where
(T.From_Date between :startdate and :enddate) or
(T.To_Date between :startdate and :enddate) or
(T.From_Date < :startdate and T.To_Date > :enddate)
Comments on this post
clivew agrees: Good catch. I was way too simple.

Last edited by majlumbo : July 12th, 2012 at 04:29 PM.

Reply With Quote
  #8  
Old July 16th, 2012, 08:13 AM
icuras icuras is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2007
Posts: 165 icuras User rank is Sergeant Major (2000 - 5000 Reputation Level)icuras User rank is Sergeant Major (2000 - 5000 Reputation Level)icuras User rank is Sergeant Major (2000 - 5000 Reputation Level)icuras User rank is Sergeant Major (2000 - 5000 Reputation Level)icuras User rank is Sergeant Major (2000 - 5000 Reputation Level)icuras User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Day 4 h 42 m 59 sec
Reputation Power: 36
re

Thanks guys. i do not know why i did not think of trying 2 between statements. but i would not have think of the last statement. appreciate all your help.

Reply With Quote
  #9  
Old July 16th, 2012, 07:02 PM
clivew clivew is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jan 2006
Location: Carlsbad, CA
Posts: 2,045 clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 37 m
Reputation Power: 382
Our pleasure.

I have been thinking about my simple query and I am not sure that it is wrong.
If you have time to test it against majlumbo's query to see if it yields the same results
or anyone else can explain where it would fail I would be grateful.
Here it is again (using parameters this time)
Code:
SELECT * FROM TABLE T
        WHERE T.FROM_DATE <= :enddate
        AND T.TO_DATE >= :startdate


Clive

Reply With Quote
  #10  
Old July 16th, 2012, 09:59 PM
majlumbo majlumbo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2008
Posts: 255 majlumbo User rank is Lance Corporal (50 - 100 Reputation Level)majlumbo User rank is Lance Corporal (50 - 100 Reputation Level)majlumbo User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 3 Days 2 m 12 sec
Reputation Power: 5
This may just work...

Reply With Quote
  #11  
Old July 20th, 2012, 03:40 AM
icuras icuras is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2007
Posts: 165 icuras User rank is Sergeant Major (2000 - 5000 Reputation Level)icuras User rank is Sergeant Major (2000 - 5000 Reputation Level)icuras User rank is Sergeant Major (2000 - 5000 Reputation Level)icuras User rank is Sergeant Major (2000 - 5000 Reputation Level)icuras User rank is Sergeant Major (2000 - 5000 Reputation Level)icuras User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Day 4 h 42 m 59 sec
Reputation Power: 36
Re

Hallo Clive

Your query might not work if i start my leave after the fromdate?

lets say i take leave from the 19th to the 21th.

I create a report from the 17th to the 25. To see whom in that period take leave...i would not be on the list... my STARTDATE is AFTER the :fromdate but my ENDDATE is smaller than my :todate.

am i right in saying that? Then majlumbo's code will work?

Thanks you guys so much for your help!!!

Reply With Quote
  #12  
Old July 20th, 2012, 12:44 PM
clivew clivew is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jan 2006
Location: Carlsbad, CA
Posts: 2,045 clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 37 m
Reputation Power: 382
Quote:
Your query might not work if i start my leave after the fromdate?
lets say i take leave from the 19th to the 21th.
I create a report from the 17th to the 25.



SQL Code:
Original - SQL Code
  1. SELECT * FROM TABLE T
  2. WHERE T.FROM_DATE <= :enddate
  3. AND T.TO_DATE >= :startdate


Yes it will.
Taking your dates:
T.FROM_DATE is 19th.
T.TO_DATE is 21st.
:enddate is 25th.
:startdate is 17th.

19 is less than 25
21 is greater than 17

Therefore both conditions are true and the record will be included.

Clive

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreDelphi Programming > Mysql between dates

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap