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

How to get specified between dates

I'm new to DB2 and I need to write a query that will allow me to find
specific dates instead of me having a date range asked for, I want it to be
calculated.
I've done this in Access by coding visual basic scripts and making monthly
reporting automatic. No longer having to change 15 queries to match
specific date ranges for the previous month is lots more efficient.
The way I coded it in Visual Basic is to make the begin date to equal the
1st of last month by subtracting a month and making the day = 1. Then for
ending date making month equal to current and day = 1 but subtracting a day.

I'd appreciate any queries or examples that could help me do this in DB2.
Thanks in advance,

David

Nov 12 '05 #1
3 13470

"David" <dw*****@sc.rr.com> wrote in message
news:fD*****************@twister.southeast.rr.com. ..
I'm new to DB2 and I need to write a query that will allow me to find
specific dates instead of me having a date range asked for, I want it to be calculated.
I've done this in Access by coding visual basic scripts and making monthly
reporting automatic. No longer having to change 15 queries to match
specific date ranges for the previous month is lots more efficient.
The way I coded it in Visual Basic is to make the begin date to equal the
1st of last month by subtracting a month and making the day = 1. Then for
ending date making month equal to current and day = 1 but subtracting a day.

Computing dates in DB2 is really not hard most of the time, but there are a
few gotchas.

Do a search on "labeled durations" in the SQL Reference for your version of
DB2 to see the basics of date calculations.

Most date computations are pretty logical and give the result you'd expect,
*except* those that involve months but that's not DB2's fault, it's the
fault of the uneven months in the Gregorian calendar.

Examples (using ISO format, i.e. YYYY-MM-DD):
'2004-01-01' + 1 year = '2005-01-01', i.e. same month and day in the next
year
'2004-01-01' + 1 day = '2004-01-02', i.e. same year and month and next day
in that month
'2004-01-01' + 1 month = '2004-02-01', i.e. same year and day in the next
month

'2004-01-31' + 1 year = '2004-01-31', i.e. same month and day in the next
year
'2004-01-31' + 1 day = '2004-02-01', i.e. first day in the next month of the
same year

So far, this is about what you'd expect. Now a gotcha:

'2004-01-31' + 1 month = ???

DB2 wants to simply add 1 month to '2004-01-31' and give you '2004-02-31'
but it knows that there is no such date as Feb 31 in any year. Therefore, it
determines that 2004 is a leap year and gives you the last possible date in
February for a leap year, i.e. '2004-02-29'. You might agree that this is
reasonable or you might not, depending on your view on how date routines
work. If this bothers you, you can always add a fixed number of days, e.g.
30 days, instead of a month and then the result will always be predictable.

Another example:

'2004-01-31' + 2 months = ???

DB2 will simply add 2 months to the first date; if the result could really
exist, that is the result you get. In other words, DB2 would calculate
'2004-03-31' and since March 31 is a perfectly possible date, that's the
result you would get.

Another gotcha:

'2004-01-31' + 1 month + 1 month = ???

While this might look the same to you and me, it is NOT the same to DB2!
This example features two additions, not one, so the additions are done
separately and using the standard order of operations. In other words

('2004-01-31' + 1 month) + 1 month = '2004-02-29'

'2004-02-29 + 1 month = ???

The result of the first addition is Feb 29 2004, because that is the last
possible day in February in a leap year. The result of the second addition
is Mar 29 (!!), 2004 because that is the same day in March as we had in
February.

Therefore:

'2004-01-31' + 2 months <> '2004-01-31' + 1 month + 1 month

Again, if that sort of result disturbs you, always add a fixed number of
days to one date to get the next date.

Rhino

Rhino


Nov 12 '05 #2

"Rhino" <rh****@NOSPAM.sympatico.ca> wrote in message
news:e5******************@news20.bellglobal.com...

"David" <dw*****@sc.rr.com> wrote in message
news:fD*****************@twister.southeast.rr.com. ..
I'm new to DB2 and I need to write a query that will allow me to find
specific dates instead of me having a date range asked for, I want it to be
calculated.
I've done this in Access by coding visual basic scripts and making monthly reporting automatic. No longer having to change 15 queries to match
specific date ranges for the previous month is lots more efficient.
The way I coded it in Visual Basic is to make the begin date to equal the 1st of last month by subtracting a month and making the day = 1. Then for ending date making month equal to current and day = 1 but subtracting a

day.

Computing dates in DB2 is really not hard most of the time, but there are

a few gotchas.

Do a search on "labeled durations" in the SQL Reference for your version of DB2 to see the basics of date calculations.

Most date computations are pretty logical and give the result you'd expect, *except* those that involve months but that's not DB2's fault, it's the
fault of the uneven months in the Gregorian calendar.

Examples (using ISO format, i.e. YYYY-MM-DD):
'2004-01-01' + 1 year = '2005-01-01', i.e. same month and day in the next
year
'2004-01-01' + 1 day = '2004-01-02', i.e. same year and month and next day
in that month
'2004-01-01' + 1 month = '2004-02-01', i.e. same year and day in the next
month

'2004-01-31' + 1 year = '2004-01-31', i.e. same month and day in the next
year
OOPS! That should be '2005-01-31'.
'2004-01-31' + 1 day = '2004-02-01', i.e. first day in the next month of the same year

So far, this is about what you'd expect. Now a gotcha:

'2004-01-31' + 1 month = ???

DB2 wants to simply add 1 month to '2004-01-31' and give you '2004-02-31'
but it knows that there is no such date as Feb 31 in any year. Therefore, it determines that 2004 is a leap year and gives you the last possible date in February for a leap year, i.e. '2004-02-29'. You might agree that this is
reasonable or you might not, depending on your view on how date routines should work. If this bothers you, you can always add a fixed number of days, e.g.
30 days, instead of a month and then the result will always be predictable.
Another example:

'2004-01-31' + 2 months = ???

DB2 will simply add 2 months to the first date; if the result could really
exist, that is the result you get. In other words, DB2 would calculate
'2004-03-31' and since March 31 is a perfectly possible date, that's the
result you would get.

Another gotcha:

'2004-01-31' + 1 month + 1 month = ???

While this might look the same to you and me, it is NOT the same to DB2!
This example features two additions, not one, so the additions are done
separately and using the standard order of operations. In other words

('2004-01-31' + 1 month) + 1 month = '2004-02-29'
Sorry, that is NOT right. I meant to leave the result as question marks,
then develop the answer in stages.
'2004-02-29 + 1 month = ???

The result of the first addition is Feb 29 2004, because that is the last
possible day in February in a leap year. The result of the second addition
is Mar 29 (!!), 2004 because that is the same day in March as we had in
February.

Therefore:

'2004-01-31' + 2 months <> '2004-01-31' + 1 month + 1 month

Again, if that sort of result disturbs you, always add a fixed number of
days to one date to get the next date.

Rhino

Nov 12 '05 #3
Thanks for the help, I finally came up with what I needed.

To get the last day of the last month, I used something like this:
Last_Day(Current_Date - 1 Month)
same as: 2004-08-31
To get the first day of the last month was a bit harder... I had to:

Last_Day(Current_Date - 1 Month) + 1 Day - 1 Month
same as: 2004-08-01
This is getting the last day of Last Month adding a day to give you the
first of this month
and then subtracting a month to give you the first of last month.

Once I came up with the formulas for these "Auto Dates" implimenting them
into my queries was easy:

Where Myfield Between Last_Day(Current_Date - 1 Month) + 1 Day - 1 Month
and
Last_Day(Current_Date - 1 Month)

Now every month I will no longer need to worry how many days are in it, I
just run the reportswith a PROC without any editing or worries! :)

"Rhino" <rh****@NOSPAM.sympatico.ca> wrote in message
news:Vs*******************@news20.bellglobal.com.. .

"Rhino" <rh****@NOSPAM.sympatico.ca> wrote in message
news:e5******************@news20.bellglobal.com...

"David" <dw*****@sc.rr.com> wrote in message
news:fD*****************@twister.southeast.rr.com. ..
> I'm new to DB2 and I need to write a query that will allow me to find
> specific dates instead of me having a date range asked for, I want it
> to

be
> calculated.
> I've done this in Access by coding visual basic scripts and making monthly > reporting automatic. No longer having to change 15 queries to match
> specific date ranges for the previous month is lots more efficient.
> The way I coded it in Visual Basic is to make the begin date to equal the > 1st of last month by subtracting a month and making the day = 1. Then for > ending date making month equal to current and day = 1 but subtracting a

day.
>

Computing dates in DB2 is really not hard most of the time, but there are

a
few gotchas.

Do a search on "labeled durations" in the SQL Reference for your version

of
DB2 to see the basics of date calculations.

Most date computations are pretty logical and give the result you'd

expect,
*except* those that involve months but that's not DB2's fault, it's the
fault of the uneven months in the Gregorian calendar.

Examples (using ISO format, i.e. YYYY-MM-DD):
'2004-01-01' + 1 year = '2005-01-01', i.e. same month and day in the next
year
'2004-01-01' + 1 day = '2004-01-02', i.e. same year and month and next
day
in that month
'2004-01-01' + 1 month = '2004-02-01', i.e. same year and day in the next
month

'2004-01-31' + 1 year = '2004-01-31', i.e. same month and day in the next
year


OOPS! That should be '2005-01-31'.
'2004-01-31' + 1 day = '2004-02-01', i.e. first day in the next month of

the
same year

So far, this is about what you'd expect. Now a gotcha:

'2004-01-31' + 1 month = ???

DB2 wants to simply add 1 month to '2004-01-31' and give you '2004-02-31'
but it knows that there is no such date as Feb 31 in any year. Therefore,

it
determines that 2004 is a leap year and gives you the last possible date

in
February for a leap year, i.e. '2004-02-29'. You might agree that this is
reasonable or you might not, depending on your view on how date routines

should
work. If this bothers you, you can always add a fixed number of days,
e.g.
30 days, instead of a month and then the result will always be

predictable.

Another example:

'2004-01-31' + 2 months = ???

DB2 will simply add 2 months to the first date; if the result could
really
exist, that is the result you get. In other words, DB2 would calculate
'2004-03-31' and since March 31 is a perfectly possible date, that's the
result you would get.

Another gotcha:

'2004-01-31' + 1 month + 1 month = ???

While this might look the same to you and me, it is NOT the same to DB2!
This example features two additions, not one, so the additions are done
separately and using the standard order of operations. In other words

('2004-01-31' + 1 month) + 1 month = '2004-02-29'

Sorry, that is NOT right. I meant to leave the result as question marks,
then develop the answer in stages.
'2004-02-29 + 1 month = ???

The result of the first addition is Feb 29 2004, because that is the last
possible day in February in a leap year. The result of the second
addition
is Mar 29 (!!), 2004 because that is the same day in March as we had in
February.

Therefore:

'2004-01-31' + 2 months <> '2004-01-31' + 1 month + 1 month

Again, if that sort of result disturbs you, always add a fixed number of
days to one date to get the next date.

Rhino


Nov 12 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: PW | last post by:
<rant> Sorry guys, but I just have to whinge. Dates in ASP are a total pain in the butt! I seem to get caught out so many times. I realise its my own fault, but going from the posts in this...
10
by: Colin Steadman | last post by:
I'm a stupid ASP programmer and I dont do Javascript (except for very simple tasks anyway), and I'm in a bit of a predicament. I've used a javascript table sorting script from here: ...
1
by: Teresa | last post by:
I need to set criteria in a query to pull all transfer dates that are between 1 year before and 6 mos before Then I need to pull all service dates that are within six months of that...
1
by: Don Sealer | last post by:
I have a report that includes 5 different subreports. I'd like to be able to open this report using a date function (Start Date and End Date). I'd like all five subreports to show the data from...
12
by: Dixie | last post by:
I am trying to calculate the number of workdays between two dates with regards to holidays as well. I have used Arvin Meyer's code on the Access Web, but as I am in Australia and my date format is...
7
by: No bother | last post by:
I have a table which has, among other fields, a date field. I want to get a count of records where certain criteria are met for, say, three days in a row. For example: NumWidgets Date...
1
by: pitfour.ferguson | last post by:
My dbase has the start date and end date of each visit. How can I ask Access to list the day of the week of the start (easy), end (easy) and, more importantly, the dates of the visit itself - ie...
2
by: Jim Carlock | last post by:
(1) Does PHP provide any way to handle dates prior to 1980? I know there's problems with Microsoft Windows NT and all Windows NT operating systems will allow a date prior to 1980 to be placed...
5
by: MRounds | last post by:
Hi Have looked through many forums but cannot find a solution for this. I have a table where i store certain dates and i want to use this table in several queries to delete records from multiple...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.