473,394 Members | 2,052 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,394 software developers and data experts.

In SQL how to determine whether a date falls within boundaries

Hi I'm struggling to find an elegant way of determining whether the date from a record falls within a number of date boundaries ranges.

For example in a separate (date boundaries) table I code the "Winter Season" running from 1/11 to 28/2 and the "Summer Season" running from 1/4 to 31/7. I have day1, day2... and month1, month2... fields for each season

I'm try to build something like Select * from tab1 where date between 1/11/year(date) and 28/2/year(date) except I get the day and month numbers from the date boundaries table using a sub select.

Using the long winded approach:


Expand|Select|Wrap|Line Numbers
  1. PARAMETERS season_requested Text ( 255 );
  2. SELECT [T\RAB Data].Species, [T\RAB Data].Date
  3. FROM [T\RAB Data]
  4. WHERE [T\RAB Data].Species='species name'
  5. AND 
  6. (Day([date]) Between (select [start day 1] from [season boundaries] where season = [season_requested]) And (select [end day 1] from [season boundaries] where season = [season_requested]) and
  7. Month([date]) Between (select [start month 1] from [season boundaries] where season = [season_requested]) And (select [end month 1] from [season boundaries] where season = [season_requested])
  8. OR
  9. Day([date]) Between (select [start day 2] from [season boundaries] where season = [season_requested]) And (select [end day 2] from [season boundaries] where season = [season_requested]) and
  10. Month([date]) Between (select [start month 2] from [season boundaries] where season = [season_requested]) And (select [end month 2] from [season boundaries] where season = [season_requested])
  11. OR
  12. Day([date]) Between (select [start day 3] from [season boundaries] where season = [season_requested]) And (select [end day 3] from [season boundaries] where season = [season_requested]) and
  13. Month([date]) Between (select [start month 3] from [season boundaries] where season = [season_requested]) And (select [end month 3] from [season boundaries] where season = [season_requested]));
but as you see its unwieldy and I'm disappearing up my ... well you know where!

Is there a better way?

Rgds, Phil
Aug 30 '10 #1
4 3575
Jerry Winston
145 Expert 100+
This sounds feasible. Can you supply sample data/table structure?

BTW, do you know if your Date fields stored as text?
Aug 30 '10 #2
Hi Jerry, many thanks for getting back on this.

the "Season Boundaries" table I've defined as:

Season - Text
Start Day 1 - Number
Start Month 1 - Number
End Day 1 - Number
End Month 1 - Number
repeat the above Number fields for Day 2 Month 2 Day 3 Month 3

The Date field from the "T\RAB Data" table is defined as Date/Time.

I define 4 "Seasons" - Summer, Winter, Combined and All in the Season Boundaries table.

Summer is defined as 1 4 31 7 0 0 0 0 0 0 0 0 so Summer starts on 1st April and ends 31st July
Winter is defined as 1 1 28 2 1 11 31 12 0 0 0 0 so Winter starts on 1st November and ends 28th February
Combined defined as 1 1 28 2 1 11 31 12 1 4 31 7 a combination of summer and winter
All defined as 1 1 31 12 0 0 0 0 0 0 0 0 gets me all records including those out of season

The query output is processed by VBA code where I originally performed the record filtering by converting dates to julian days which worked fine. However, I am being requested to bring this date processing into the query which makes some sense and has benefits for me also but its proving harder than I envisaged - I don't know enough advanced SQL and its difficult to find an exact example on the web - it sounded simple enough !!

I reckon, the SQL code I detailed in my first append will only work for all situations if I supply start day/month end day/month fields for each month in the season range (esp winter as it overlaps year boundaries).

I hope this is making sense!!

BTW where do Microsoft provide a comprehensive SQL Language Reference for Access? I see lots of blog entries that complain of the difficulty of finding this "first principles" info. Or do you have to buy a book? Because MSDN provide comprehensive VB and VBA documentation online I assumed there would be an SQL equivalent.

Kind Rgds, Phil
Aug 30 '10 #3
Jerry, I think I may have answered my own question to a certain extent. Writing my last append prompted me to think about an SQL method of determining a Julian Date or day number for the year from the date in the record and season baoundaries table.

I found the DATEPART function which streamlines the processing for me.

I now defined Start Date 1/End Date 1 Start Date 2/End Date 2 etc as Date/Time in the "Season Boundaries" tables and code the where clause thus:

WHERE ((([T\RAB Data].Date) Between [Date1] And [Date 2])
AND
((DatePart("Y",[date])) Between (select datepart("Y",[start date 1]) from [season boundaries] where season = [season_requested]) And (select datepart("Y",[end date 1]) from [season boundaries] where season = [season_requested])
Or
(DatePart("Y",[date])) Between (select datepart("Y",[start date 2]) from [season boundaries] where season = [season_requested]) And (select datepart("Y",[end date 2]) from [season boundaries] where season = [season_requested])
Or
(DatePart("Y",[date])) Between (select datepart("Y",[start date 3]) from [season boundaries] where season = [season_requested]) And (select datepart("Y",[end date 3]) from [season boundaries] where season = [season_requested])))

This works although the performance is noticeably degraded and its still rather inelegant but I think I can run with it.

If you do have a better way I'd be interested.

Many thanks, Phil
Aug 31 '10 #4
Have now added extra code to cater for leap year requirements. Here is the entire Query which extracts counts of bird populations by season and where the seasons are defined in a separate table with three date ranges specified for the year 2000 (a leap year). Hopefully this gives something back to this excellent forum. Can it be bettered?

Expand|Select|Wrap|Line Numbers
  1.  PARAMETERS Species_Requested Text ( 255 ), Date1 DateTime, Date2 DateTime, Season_Requested Text ( 255 );
  2. SELECT Max([T\RAB Data].Meancount) AS Largest_Count, [T\RAB Data].Species AS SpName, [T\RAB Data].Square, [T\RAB Data].Tetrad, [T\RAB Data].Speccode AS SpCode
  3. FROM [T\RAB Data]
  4. WHERE ((([T\RAB Data].Date) Between [Date1] And [Date2]) 
  5. AND   
  6. (iif(datepart("Y",[date]) > 59, DatePart("Y",[date])+(29-Day(DateSerial(Year([date]),3,0))), DatePart("Y",[date])) Between (select datepart("Y",[start date 1])  from [season boundaries] where season = [season_requested]) And 
  7. (select datepart("Y",[end date 1])  from [season boundaries] where season = [season_requested])
  8. or 
  9. iif(datepart("Y",[date]) > 59, DatePart("Y",[date])+(29-Day(DateSerial(Year([date]),3,0))), DatePart("Y",[date])) Between (select datepart("Y",[start date 2])  from [season boundaries] where season = [season_requested]) And 
  10. (select datepart("Y",[end date 2])  from [season boundaries] where season = [season_requested])
  11. or 
  12. iif(datepart("Y",[date]) > 59, DatePart("Y",[date])+(29-Day(DateSerial(Year([date]),3,0))), DatePart("Y",[date])) Between (select datepart("Y",[start date 3])  from [season boundaries] where season = [season_requested]) And 
  13. (select datepart("Y",[end date 3])  from [season boundaries] where season = [season_requested])))
  14. GROUP BY [T\RAB Data].Species, [T\RAB Data].Square, [T\RAB Data].Tetrad, [T\RAB Data].Speccode
  15. HAVING ((([T\RAB Data].Species) Like [Species_Requested] & "*"))
  16. ORDER BY Max([T\RAB Data].Meancount), [T\RAB Data].Species, [T\RAB Data].Square, [T\RAB Data].Tetrad;
  17.  
Sep 3 '10 #5

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

Similar topics

2
by: OvErboRed | last post by:
Hi, I'm trying to determine whether a given URL exists. I'm new to Python but I think that urllib is the tool for the job. However, if I give it a non-existent file, it simply returns the 404 page....
16
by: Donjuan | last post by:
Hi all I have trouble with tracking whether my image file is loaded. i use DHTML to change my image. HERE is the code: <img name="someimage" src="1.jpg"...
1
by: Ashabul Yeameen | last post by:
I am writing a program which at some point need to call fputwc() function. But since I want to make this code portable I've decided to write my own fputwc() funcion where the compiler doesn't...
2
by: Shravan | last post by:
Hi, In my application I need to determine whether Excel is installed on my system or not. Can anybody tell me how to check that. Thanks, Shravan.
1
by: topramen | last post by:
does any one here know of a good way to to determine whether or not a given path is a directory (e.g., "c:\mydir") or a file (e.g., "c:\mydir \myfile.txt")? i started attacking this problem by...
1
by: DeanMiller | last post by:
I was wondering if there is an easy way to check if a date falls in a given selection range. I have the selection range already made and I want to be able to take a given date and see if it falls...
9
by: Mark Berry | last post by:
Hi, How can I determine whether an object is derived from another object? My specific example is that I have a CustomError class with several specific error types that derive from it...
13
by: citizenprice | last post by:
I have three fields in a Union Access Query (ID, Begin_Date, End_Date). I want to modify the query to include a parameter date to be entered by the user and produce a result if the parameter date...
4
by: Chris Davies | last post by:
I'm curious. I'd like to determine whether a particular URI scheme (the "ftp:", "news:", "http:" part) is available within the user's browser. For example, consider this HTML snippet: ...
3
by: abhimanyu | last post by:
I have a method Marshal.IsComObject(...) that returns TRUE if an object is a COM object. I have an object of Excel.Worksheet that I released using Marshal.ReleaseComObject(...). I want to iterate...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.