473,785 Members | 2,843 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Selecting from a date range

3 New Member
Apolgies if this has already been answered...

Right, I have a season table where I specify each season (Winter, Summer, Spring etc.) with a 'date from' and 'date to' field which is formatted as dd/mm (no year).

I also have a property table that defines a properties grade, I also have a table used to define each grade ('Grade', 'Description')

Then finally I have a relationship table which covers 'Grade', 'Season', and 'Price'.

I need a query that can look at the 'Date_From' in my Booking table, select the correct season, and look at the properties grade to select the right price for the correct season and grade...

I'm compleaty stumped on this one, and any help would be much apprecieated!

thanks,
Luke
Feb 26 '07 #1
8 2365
willakawill
1,646 Top Contributor
Hi. Would you submit the schema for this database. The table names and column names and data types. thanks
Feb 26 '07 #2
luke64
3 New Member
tblProperty
Property_Name = text
Property_ID = autonumber
Grade = text
Address = text

tblBooking
Customer_ID = integer
Property_ID = integer
Booking_ID = autonumber
Date_From = short date
Date_To = short date

tblGrade
Grade = text
Description = text

tblRentalPrice
Grade = text
Season = text
Price = currency

tblSeason
Season = text
Date_From = date (formatted as "dd/mm")
Date_To = date (formatted as "dd/mm")
Feb 27 '07 #3
willakawill
1,646 Top Contributor
tblProperty
Property_Name = text
Property_ID = autonumber
Grade = text
Address = text

tblBooking
Customer_ID = integer
Property_ID = integer
Booking_ID = autonumber
Date_From = short date
Date_To = short date

tblGrade
Grade = text
Description = text

tblRentalPrice
Grade = text
Season = text
Price = currency

tblSeason
Season = text
Date_From = date (formatted as "dd/mm")
Date_To = date (formatted as "dd/mm")
I guess your problem is in bringing together the season stuff to match up with the booking date. Try this out. In your sql statement format both as julian dates. i.e. the day of the year from 1 to 365.
Expand|Select|Wrap|Line Numbers
  1. FORMAT([tblSeason].[Date_From], "y") AS DateFromDay
You will find it easier to compare dates this way.
Feb 27 '07 #4
luke64
3 New Member
Thanks for replying, what I'm having most trouble is actually writing the query to to do this, I'm very new to SQL, and before only created queries via the design view.

I was thinking along the lines of using the SQL BETWEEN function, but I'm unsure how to specifiy each date range within my seasons table to allow the query to return the season.

My VB skills tell me I should just code a solution, but I'm learning SQL, not VB!!
Mar 1 '07 #5
willakawill
1,646 Top Contributor
Thanks for replying, what I'm having most trouble is actually writing the query to to do this, I'm very new to SQL, and before only created queries via the design view.

I was thinking along the lines of using the SQL BETWEEN function, but I'm unsure how to specifiy each date range within my seasons table to allow the query to return the season.

My VB skills tell me I should just code a solution, but I'm learning SQL, not VB!!
I understand this difficulty. My previous reply was in fact one solution but you will have to take it a step or 2 further to make it work.
Because your date formats are different, if you convert them to julian date format you can use BETWEEN quite easily. As an example you might have spring listed as from april 5th to june 8th. 95th day of the year to 159th day of the year. You need to check what season april 12th 2007 falls in. Well that is 102nd day of the year so is 102 between 95 and 159? Easy.
Mar 2 '07 #6
NeoPa
32,578 Recognized Expert Moderator MVP
Apolgies if this has already been answered...

Right, I have a season table where I specify each season (Winter, Summer, Spring etc.) with a 'date from' and 'date to' field which is formatted as dd/mm (no year).

I also have a property table that defines a properties grade, I also have a table used to define each grade ('Grade', 'Description')

Then finally I have a relationship table which covers 'Grade', 'Season', and 'Price'.

I need a query that can look at the 'Date_From' in my Booking table, select the correct season, and look at the properties grade to select the right price for the correct season and grade...

I'm compleaty stumped on this one, and any help would be much apprecieated!

thanks,
Luke
On a strictly SQL basis, and comparing the Date_From field of tblBooking, check this out and see if it illustrates the concept for you :
Expand|Select|Wrap|Line Numbers
  1. SELECT tblBooking.*,
  2.        tblSeason.Text
  3. FROM tblBooking, tblSeason
  4. WHERE tblBooking.From_Date Between
  5.       tblSeason.Date_From And tblSeason.Date_To
It uses an Outer Join (no join) but drops any results where the season is not matched.
Mar 2 '07 #7
willakawill
1,646 Top Contributor
On a strictly SQL basis, and comparing the Date_From field of tblBooking, check this out and see if it illustrates the concept for you :
Expand|Select|Wrap|Line Numbers
  1. SELECT tblBooking.*,
  2.        tblSeason.Text
  3. FROM tblBooking, tblSeason
  4. WHERE tblBooking.From_Date Between
  5.       tblSeason.Date_From And tblSeason.Date_To
It uses an Outer Join (no join) but drops any results where the season is not matched.
I don't think this will work becuase the season dates do not have a year, and if they did it could be any year so it would not match with the booking from date
Mar 2 '07 #8
NeoPa
32,578 Recognized Expert Moderator MVP
You're absolutely right of course Will. I'm sure this was discussed earlier too so I had no excuse for that oversight. Let's see if I can make up for that lapse in concentration. Extra complications as one season probably wraps around the end of the year :( Not kidding, this complicates matters more than I'd expected.
This means of course, that the data is not stored correctly. It doesn't make much sense to store MM/DD data in a Date/Time field as that must contain a year part. It's a possibility to store it that way, but all code would have to know to interpret it without the year part (Simply formatting it that way will not suffice). Julian date format, though a very clever idea, will also not work perfectly, as February will confuse things for dates that follow in leap years (It'll be a fairly good approximation mind).
Expand|Select|Wrap|Line Numbers
  1. SELECT B.*,
  2.        S.Text
  3. FROM tblBooking AS B, tblSeason AS S
  4. WHERE (((S.Date_To>S.Date_From)
  5.   AND (Format(B.From_Date,'mmdd') Between
  6.       Format(S.Date_From,'mmdd') And Format(S.Date_To,'mmdd')))
  7.    OR ((S.Date_To<S.Date_From)
  8.   AND (Format(B.From_Date,'mmdd') Not Between
  9.       Format(S.Date_From,'mmdd') And Format(S.Date_To,'mmdd'))))
For this to work as is, all the dates set up in tblSeasons must share the same year (Even though it's never displayed or even used explicitly).
Mar 2 '07 #9

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

Similar topics

1
3349
by: Jeff Roe | last post by:
I need to do a search in MySql for birth date range. (i.e. for birth date >= 01/23/65 and <=12/31/03.) Any suggestions on how to do this select? Thanks!
1
6762
by: Sunny K | last post by:
Hi, I am having a problem with aquery. Firstly here is a script to create the table and insert some sample data: CREATE TABLE . ( (17) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , NOT NULL , NOT NULL
1
6110
by: John Rappold | last post by:
I've Googled this, but can't find an answer to my problem. When a user selects a page with the recordset I want it to list dates that fall into a certain range, related to CURDATE(). The range fields are start_date and end_date, which are both DATETIME column types. Using BETWEEN won't work because it won't list a date that starts BEFORE CURDATE() e.g. If CURDATE() is 2005-08-10
3
1519
by: Not Me | last post by:
Hi, Not sure if this would be possible without using vba or similar programming, but can I use sql to create a column of dates in a regular sequence (such as weekly, monthly etc.) I would like the results to be e.g. date 01/01/04
2
2002
by: Catherine | last post by:
I have a database where in my query I need to be able to locate accounts that show a date greater than 14 days from the current date. How would I structure this to allow for all accounts that over 14 days old from today's date? Any help you can give me would be greatly appreciated.
5
5557
by: Miquel van Smoorenburg | last post by:
I have a database with a btree index on the 'removed' field, which is of type 'date'. However it isn't being used: techdb2=> explain select * from lines where removed > CURRENT_DATE; QUERY PLAN ------------------------------------------------------------ Seq Scan on lines (cost=0.00..243.47 rows=2189 width=324) Filter: (removed > ('now'::text)::date) (2 rows)
2
2605
by: movieking81 | last post by:
If someone could help me with this, that would be great. I need to select a number of records from an SQL table based on a date range, so I started with this select. <html> <code> resultssql = "SELECT * FROM testtable where name = '" & request("name") & "' AND fromd >= '" & getdatefrom & "' AND fromd <= '" & getdateto & "'" </code> </html> This select does find records, however they are not the correct ones. The records it finds only...
4
3621
by: Eugene Anthony | last post by:
I have a table that has a DateTime column which uses a DataTime datatype. How do I retrieve a range of records based on the month and year using ms sql? Eugene Anthony *** Sent via Developersdex http://www.developersdex.com ***
5
2890
by: megahurtz | last post by:
I need to put together an SQL statement and I can't think of how to make it work properly. The scenario is that I have news items in a database that have a launch time and can optionally have an expire time set. I want to select all records that are above the launch time and below their expire time (if it is set). What I have so far is: $rightnow = mktime(); SELECT * FROM teamshadow_news WHERE (launch_time <= $rightnow AND expire_time =...
9
1562
by: trixxnixon | last post by:
this may not make any sense, but here goes... i have a form based on a query that pulls its paramater criteria from drop down boxes on a form. im using a date range, and i can get this to run correctly, but i would like the reoprt to show an infinite date range if the criteria(start date and end date) is left blank. does anyone have any idea what im talking about?
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10329
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10152
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9950
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8974
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3650
muto222
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.