473,378 Members | 1,478 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,378 software developers and data experts.

Calculate year range function

I'm trying to write a function to be used in a query - because I don't think it can be compiled directly in a query (using Expression Builder). This is for a holiday DB. I would like to determine and automatically calculate/ display which holiday year annual leave dates fall into, i.s. if date is 3 Feb 07 that would fall into holiday year 2006/2007, whereas 8 Mar 07 would fall into 2007/2008. Basically in English I think it would be:

Find Year [EndDate] = 'YearIn'
If Date [EndDate] is before 01 March 'YearIn'
Then
'YearRange' = 'YearIn' minus 1 / 'YearIn' (the / being separator not division)
Else
'YearRange' = 'YearIn' / 'YearIn' plus 1

Thanks!
Feb 11 '07 #1
9 3541
ADezii
8,834 Expert 8TB
I'm trying to write a function to be used in a query - because I don't think it can be compiled directly in a query (using Expression Builder). This is for a holiday DB. I would like to determine and automatically calculate/ display which holiday year annual leave dates fall into, i.s. if date is 3 Feb 07 that would fall into holiday year 2006/2007, whereas 8 Mar 07 would fall into 2007/2008. Basically in English I think it would be:

Find Year [EndDate] = 'YearIn'
If Date [EndDate] is before 01 March 'YearIn'
Then
'YearRange' = 'YearIn' minus 1 / 'YearIn' (the / being separator not division)
Else
'YearRange' = 'YearIn' / 'YearIn' plus 1

Thanks!
Here is the Function that will do the job, its associated call, and output:
Expand|Select|Wrap|Line Numbers
  1. Public Function fCalculateHolidayYears(MyDate As Date) As String
  2.   If MyDate < CDate("3/1/" & Year(MyDate)) Then
  3.     fCalculateHolidayYears = Year(MyDate) - 1 & "/" & Year(MyDate)
  4.   Else
  5.     fCalculateHolidayYears = Year(MyDate) & "/" & Year(MyDate) + 1
  6.   End If
  7. End Function
Debug.Print fCalculateHolidayYears(#3/8/2007#)
fCalculateHolidayYears([Leave Date]) 'if used in a Calculated Field

Output: 2007/2008
Feb 11 '07 #2
NeoPa
32,556 Expert Mod 16PB
I'm trying to write a function to be used in a query - because I don't think it can be compiled directly in a query (using Expression Builder). This is for a holiday DB. I would like to determine and automatically calculate/ display which holiday year annual leave dates fall into, i.s. if date is 3 Feb 07 that would fall into holiday year 2006/2007, whereas 8 Mar 07 would fall into 2007/2008. Basically in English I think it would be:

Find Year [EndDate] = 'YearIn'
If Date [EndDate] is before 01 March 'YearIn'
Then
'YearRange' = 'YearIn' minus 1 / 'YearIn' (the / being separator not division)
Else
'YearRange' = 'YearIn' / 'YearIn' plus 1

Thanks!
In SQL it would be quite straightforward actually.
Say your date field is [MyDate] :
Expand|Select|Wrap|Line Numbers
  1. Year([MyDate]) - IIf(Month([MyDate])<3,1,0) AS HolidayYear
Feb 11 '07 #3
ADezii
8,834 Expert 8TB
In SQL it would be quite straightforward actually.
Say your date field is [MyDate] :
Expand|Select|Wrap|Line Numbers
  1. Year([MyDate]) - IIf(Month([MyDate])<3,1,0) AS HolidayYear
Thanks for the pointer. I was under the impression that he wanted the Holiday Year returned as a String in the Format YYYY/YYYY (2006/2007). I try to avoid the IIF() Function at all costs. It is my understanding that it is notoriously inefficient and slow to execute. Please get back to me with your opinion on this.
Feb 12 '07 #4
Thanks peeps: NeoPa and ADezii. Yes, I did want the year returned in text format. I'll try both these out and get back to you.
Feb 12 '07 #5
NeoPa
32,556 Expert Mod 16PB
Thanks for the pointer. I was under the impression that he wanted the Holiday Year returned as a String in the Format YYYY/YYYY (2006/2007). I try to avoid the IIF() Function at all costs. It is my understanding that it is notoriously inefficient and slow to execute. Please get back to me with your opinion on this.
Any function executed in SQL will be relatively slow.
The special innefficiency of the IIf() function is that it resolves all three parameters before it decides on which value to return. In some cases that can be a very heavy overhead, depending on what's in there. I can't give you the link (I found it within a link from this site though - Tutorials thread I think) but one of the MVPs has even recommended the use of a bespoke, more efficient, version of this function.
Personally, I think that a religious approach to the situation is generally uncalled for. Except in certain extreme cases (normally found in large data processing rather than form type manipulation) the extra delay is negligible.
Feb 12 '07 #6
NeoPa
32,556 Expert Mod 16PB
Thanks peeps: NeoPa and ADezii. Yes, I did want the year returned in text format. I'll try both these out and get back to you.
You may want to let us know which of the two formats (YYYY or YYYY/YYYY) you were after too ;)
Feb 12 '07 #7
ADezii
8,834 Expert 8TB
Any function executed in SQL will be relatively slow.
The special innefficiency of the IIf() function is that it resolves all three parameters before it decides on which value to return. In some cases that can be a very heavy overhead, depending on what's in there. I can't give you the link (I found it within a link from this site though - Tutorials thread I think) but one of the MVPs has even recommended the use of a bespoke, more efficient, version of this function.
Personally, I think that a religious approach to the situation is generally uncalled for. Except in certain extreme cases (normally found in large data processing rather than form type manipulation) the extra delay is negligible.
Thanks for the feedback.
Feb 13 '07 #8
Hi peeps, many apologies firstly that I haven't got back to you for so long (busy!)

I managed to use the code from NeoPa as a basis with pointer from Dezi and 'cobbled' together what I needed in a calculated query field. When I'm free again I'll post the code/ query set-up I used, just for you to see - but it works a dream for what I wanted and couldn't have done it without your help!

Thanks! ;-)
Mar 1 '07 #9
NeoPa
32,556 Expert Mod 16PB
I'm very pleased you're sorted (and the delay posting is not a problem - we understand that people have lives) :)
Please do post the set-up you used that resolved your problem. This always helps other members (of TSDN and the public) when they come looking for solutions.
Mar 1 '07 #10

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

Similar topics

5
by: Tencip | last post by:
Hi everyone, I'm trying to build a simple script that does the following. It should find today's month and year, and then go into a DB query string and look for all records that are from this...
4
by: John Hunter | last post by:
>>> from datetime import date >>> dt = date(1005,1,1) >>> print dt.strftime('%Y') Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: year=1005 is before 1900; the...
4
by: Jan Szymczuk | last post by:
I'm creating an MS Access 2000 database where I have a number of people entered using simple basic fields, Surname: SMITH Forenames: John DoB: 09/09/1958 Age:...
11
by: Laery | last post by:
Hi, I'm currently adding a new module to an old borland C3.1 application (dos). And I need to calculate a date by subtracting the number of days from a given date. I know I could use an...
7
by: Adrian | last post by:
I hit on this problem converting a VB.NET insurance application to C#. Age next birthday calculated from date of birth is often needed in insurance premium calculations. Originally done using...
5
by: Beemer Biker | last post by:
I cant seem to get that date into any DateTime to make my calculation directly by subtracting "01-01-0000" from "now". After reading this:...
4
by: Vince | last post by:
Given a week Number, how do I calculate the date that for the Monday of that week?
15
by: student4lifer | last post by:
Hello, I have 2 time fields dynamically generated in format "m/d/y H:m". Could someone show me a good function to calculate the time interval difference in minutes? I played with strtotime() but...
5
FishVal
by: FishVal | last post by:
IMHO, the following is not a how-to-do instruction to solve a particular problem but more a concept-proof stuff demonstrating possibilities of SQL. So, let us say the problem is to calculate...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.