473,387 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Selecting from a date range

3
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 2338
willakawill
1,646 1GB
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
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 1GB
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
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 1GB
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,556 Expert Mod 16PB
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 1GB
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,556 Expert Mod 16PB
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
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
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 , ...
1
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...
3
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...
2
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...
5
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...
2
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 =...
4
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...
5
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...
9
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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...

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.