473,811 Members | 2,717 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 13506

"David" <dw*****@sc.rr. com> wrote in message
news:fD******** *********@twist er.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******** **********@news 20.bellglobal.c om...

"David" <dw*****@sc.rr. com> wrote in message
news:fD******** *********@twist er.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(Curren t_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(Curren t_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(Curren t_Date - 1 Month) + 1 Day - 1 Month
and
Last_Day(Curren t_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******** ***********@new s20.bellglobal. com...

"Rhino" <rh****@NOSPAM. sympatico.ca> wrote in message
news:e5******** **********@news 20.bellglobal.c om...

"David" <dw*****@sc.rr. com> wrote in message
news:fD******** *********@twist er.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
2146
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 newsgroup and others, I'm not the only one. Its just a poorly addressed issue within ASP. So for all your poor buggers out there that are having problems, particularly with european date formats, here is my solution. I have the user enter their...
10
3301
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: http://www.ipwebdesign.net/kaelisSpace/useful_tableSort.html This works great except it doesn't sort my UK formatted dates properly, and I end up with something like this: Birth Date (dd/mm/yyyy)
1
2827
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 transfer date.. can anyone help.. i've tried a few things and nothing seems to work just right. I tried getting all transfer dates..
1
2132
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 those dates. I have learned how to use this start/end function but I can't figure out how to use it with these subreports. Right now the report opens but asks for start/end dates for each of the five reports. Is there a way I can input one set of...
12
2450
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 dd/mm/yyyy, I have found that the dates I put in my holidays table are reversed into American dates. So, the wrong holiday dates are subtracted from the total workdays between the start and end dates. Is there an easy fix for this? ******Code...
7
3358
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 1/1/2000 10 1/2/2000 20 1/3/2000 10 1/4/2000 15 1/5/2000
1
15806
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 list the dates between the start and end date. Basically I am doing a day of the week analysis and need to know which day of the week is busiest.
2
2201
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 upon a file, but it ends up not being able to display the date (probably an Windows Explorer specific issue). (2) I'm looking for for ways to store dates and process dates (dates only). This probably ends up as a database specific issue, so I'm...
5
9682
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 tables by using a macro to run the queries. I can use DISTINCTROW to delete rows that are equal to a date but cannot figure out how to delete rows that are between a range of dates (stored in . Example of my query : DELETE...
0
9728
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
9605
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
10648
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
10389
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...
1
10402
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10135
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
9205
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
5554
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3018
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.