473,715 Members | 2,138 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

YYYYMM "between" selection on date column?

Hi there,

we're currently convert an old ISAM application to Java and DB2.

Among the load of problems we found the reports which have user
selections like "select all data between mm/yyyy and mm/yyyy".

Means: if the user enters "10/2004 - 10/2005" everything between
"2004-10-01" and "2004-10-31" is selected.

This isn't a real problem because I can expand the lower and upper
MMYYYY date selection to a DB2 date by calculating the first and last
day of a month and stuff it into the query parameters.

Or calculating the Julian date and substract 1 day from the upper
selection value and use this as parameter.

But I'm curious if this can be solved in a query without consuming too
much load on the database server.

A "select ... where order_date >= '2004-10' and order_date <='2005-10'
didn't work, >='2004-10-*' neither :-(

Bernd

--
"Ja, alles meine Herren" sprach Fürst Lichnowsky. "Ooch det roochen?"
"Ja, auch das Rauchen." "Ooch im Tiergarten?" "Ja, auch im Tiergarten
darf geraucht werden, meine Herren." Und so endeten die Barrikadenkämpf e
des 18. März in Berlin
Nov 12 '05 #1
16 9316
Bernd Hohmann wrote:
Hi there,

we're currently convert an old ISAM application to Java and DB2.

Among the load of problems we found the reports which have user
selections like "select all data between mm/yyyy and mm/yyyy".

Means: if the user enters "10/2004 - 10/2005" everything between
"2004-10-01" and "2004-10-31" is selected.

This isn't a real problem because I can expand the lower and upper
MMYYYY date selection to a DB2 date by calculating the first and last
day of a month and stuff it into the query parameters.

Or calculating the Julian date and substract 1 day from the upper
selection value and use this as parameter.

But I'm curious if this can be solved in a query without consuming too
much load on the database server.

A "select ... where order_date >= '2004-10' and order_date <='2005-10'
didn't work, >='2004-10-*' neither :-(

Bernd

Bernd, why do you believe there is load on the system? when doing the
math you propose. Depending on your fixpack you will either get the
constant expressions involved executed exactly once at runtime, or if
you pass true constants and are on a late fixpack DB2 may compute the
whole beast at compile-time (sonething we call "constant folding").

Cheers
Serge

PS: Welcome back from whichever stone you have been hiding under ;-)
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #2
Serge Rielau wrote:
This isn't a real problem because I can expand the lower and upper
MMYYYY date selection to a DB2 date by calculating the first and last
day of a month and stuff it into the query parameters.

Or calculating the Julian date and substract 1 day from the upper
selection value and use this as parameter.
[...] Bernd, why do you believe there is load on the system? when doing the
math you propose. Depending on your fixpack you will either get the
constant expressions involved executed exactly once at runtime, or if
you pass true constants and are on a late fixpack DB2 may compute the
whole beast at compile-time (sonething we call "constant folding").
The problem isn't really the CPU load but the problem of additional
code. Additional code means problems in maintenance sooner or later.

So I have to decide betwen pest and cholera. The old ISAM was ok for
this task: "select ... where o_date between 200410 and 200510*".
PS: Welcome back from whichever stone you have been hiding under ;-)


Thanks a lot. I think I'll participate more in the next weeks to refresh
my knowledge.

Bernd

--
"Ja, alles meine Herren" sprach Fürst Lichnowsky. "Ooch det roochen?"
"Ja, auch das Rauchen." "Ooch im Tiergarten?" "Ja, auch im Tiergarten
darf geraucht werden, meine Herren." Und so endeten die Barrikadenkämpf e
des 18. März in Berlin
Nov 12 '05 #3
Bernd Hohmann wrote:
Hi there,

we're currently convert an old ISAM application to Java and DB2.

Among the load of problems we found the reports which have user
selections like "select all data between mm/yyyy and mm/yyyy".

Means: if the user enters "10/2004 - 10/2005" everything between
"2004-10-01" and "2004-10-31" is selected.

This isn't a real problem because I can expand the lower and upper
MMYYYY date selection to a DB2 date by calculating the first and last
day of a month and stuff it into the query parameters.

Or calculating the Julian date and substract 1 day from the upper
selection value and use this as parameter.

But I'm curious if this can be solved in a query without consuming too
much load on the database server.

A "select ... where order_date >= '2004-10' and order_date <='2005-10'
didn't work, >='2004-10-*' neither :-(

Bernd

It is ugly looking but
YEAR(date_col)| |'/'||MONTH(date_c ol) BETWEEN 'yyyy/mm' AND 'yyyy/mm'
should work, with the proviso that if it is the only WHERE clause, a
sequential scan of your table may be necessitated
Nov 12 '05 #4
Bernd Hohmann wrote:
Serge Rielau wrote:
This isn't a real problem because I can expand the lower and upper
MMYYYY date selection to a DB2 date by calculating the first and last
day of a month and stuff it into the query parameters.

Or calculating the Julian date and substract 1 day from the upper
selection value and use this as parameter.


[...]
Bernd, why do you believe there is load on the system? when doing the
math you propose. Depending on your fixpack you will either get the
constant expressions involved executed exactly once at runtime, or if
you pass true constants and are on a late fixpack DB2 may compute the
whole beast at compile-time (sonething we call "constant folding").

The problem isn't really the CPU load but the problem of additional
code. Additional code means problems in maintenance sooner or later.

So I have to decide betwen pest and cholera. The old ISAM was ok for
this task: "select ... where o_date between 200410 and 200510*".

Well, you can alwasy use pandora's box:
CREATE FUNCTION my_date(arg INT)
RETURNS DATE DETERMINISTIC NO EXTERNAL ACTION CONTAINS SQL
RETURN <pestandcholera >

Would be fun to see the plan If all goes well: *poof* Out comes a constant.

Cheers
Serge

--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #5
Bob Stearns wrote:
It is ugly looking but
YEAR(date_col)| |'/'||MONTH(date_c ol) BETWEEN 'yyyy/mm' AND 'yyyy/mm'
should work, with the proviso that if it is the only WHERE clause, a
sequential scan of your table may be necessitated


Well, if its doing the job I'll try it <g>

Bernd

--
"Ja, alles meine Herren" sprach Fürst Lichnowsky. "Ooch det roochen?"
"Ja, auch das Rauchen." "Ooch im Tiergarten?" "Ja, auch im Tiergarten
darf geraucht werden, meine Herren." Und so endeten die Barrikadenkämpf e
des 18. März in Berlin
Nov 12 '05 #6
Bernd Hohmann wrote:
Bob Stearns wrote:
It is ugly looking but
YEAR(date_col)| |'/'||MONTH(date_c ol) BETWEEN 'yyyy/mm' AND 'yyyy/mm'
should work, with the proviso that if it is the only WHERE clause, a
sequential scan of your table may be necessitated

Well, if its doing the job I'll try it <g>

Bernd

*grmbl* Don't complain if it's slow....

DROP FUNCTION date_begin;
CREATE FUNCTION date_begin(arg VARCHAR(7))
DETERMINISTIC NO EXTERNAL ACTION CONTAINS SQL
RETURNS DATE
RETURN DATE(SUBSTR(arg , 1, 3) || '01/' || SUBSTR(arg, 4, 4));

DROP FUNCTION date_end;
CREATE FUNCTION date_end(arg VARCHAR(7))
DETERMINISTIC NO EXTERNAL ACTION CONTAINS SQL
RETURNS DATE
RETURN DATE(SUBSTR(arg , 1, 3) || '01/' || SUBSTR(arg, 4, 4)) + 1 MONTH
-1 DAY;

DROP TABLE T;
CREATE TABLE T (dt DATE);
INSERT INTO T VALUES
('10/01/2000'),
('09/30/2001'),
('08/15/2002'),
('07/12/2001');

SELECT * FROM T WHERE dt BETWEEN date_begin('09/2000') AND
date_end('08/2002');

Original Statement:
------------------
SELECT *
FROM T
WHERE dt BETWEEN date_begin('09/2000') AND date_end('08/2002')
Optimized Statement:
-------------------
SELECT Q1.DT AS "DT"
FROM SRIELAU.T AS Q1
WHERE (Q1.DT <= -(+(DATE(('08/' || '01/' || '2002')), 1, 2), 1, 3)) AND
(DATE(('09/' || '01/' || '2000')) <= Q1.DT)

Access Plan:
-----------
Total Cost: 12.875
Query Degree: 1

Rows
RETURN
( 1)
Cost
I/O
|
0.4
TBSCAN
( 2)
12.875
1
|
4
TABLE: SRIELAU
T
Give this puppy an index on DT and it will fly with a start-stop key

Cheers
Serge

--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #7
Serge Rielau wrote:
Give this puppy an index on DT and it will fly with a start-stop key


Works fine, but now the trouble with functions begin.

The user connected is "benutzer", the function is created with schema "cmm".

"...date_begin( '09/2004')" doesn't work but
"...cmm.date_be gin('09/2004')" does.

How can I add "cmm" permanent to the function path?

Bernd

--
"Ja, alles meine Herren" sprach Fürst Lichnowsky. "Ooch det roochen?"
"Ja, auch das Rauchen." "Ooch im Tiergarten?" "Ja, auch im Tiergarten
darf geraucht werden, meine Herren." Und so endeten die Barrikadenkämpf e
des 18. März in Berlin
Nov 12 '05 #8
Bernd Hohmann wrote:
Serge Rielau wrote:
Give this puppy an index on DT and it will fly with a start-stop key


Works fine, but now the trouble with functions begin.

The user connected is "benutzer", the function is created with schema
"cmm".

"...date_begin( '09/2004')" doesn't work but
"...cmm.date_be gin('09/2004')" does.

How can I add "cmm" permanent to the function path?


Modify the CURRENT FUNCTION PATH special register. I believe if your
application uses JDBC/CLI, then you can set the function path also in the
cli config.

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Nov 12 '05 #9
Bernd Hohmann wrote:
Serge Rielau wrote:
Give this puppy an index on DT and it will fly with a start-stop key

Works fine, but now the trouble with functions begin.

The user connected is "benutzer", the function is created with schema
"cmm".

"...date_begin( '09/2004')" doesn't work but
"...cmm.date_be gin('09/2004')" does.

How can I add "cmm" permanent to the function path?

Bernd

SET PATH = CURRENT PATH, CMM
Permatent for the session. You can also add it to your client connectuon
properties, like the cli.ini file...

--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #10

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

Similar topics

2
2028
by: Kevin | last post by:
When I run the query (please see below) between 08:30:00 and 17:29:59, I get the data from row 1; if I run the same query between 17:30:00 and 08:29:59, I get the "empty set." Can anyone explain why? Many thanks! Table SQL:
4
3341
by: Greg Iocco | last post by:
Simple problem, but I've checked the newsgroups and couldn't find an answer. On the main swithboard is a command button to open a report. The report I built is based off a query, and the query is now restricted by the simple "Between And ". What I want to do is pass the values of the two parameters into some textboxes in the heading of the report (so if 12/1/03 and 12/31/03 are the parameters, I can display the entered dates in the...
1
4652
by: Jaycee66 | last post by:
Access 2000 "Between" query question: I am relatively new to MS Access and everytime I think I have a basic concept of the program it pokes me in the eye with something I think would be simple to create. I want to create 2 separate queries for a label. I would like to have the query prompt me for the date I choose to start from until the present date and the other to prompt me for the date I choose to start from and 2 additional numbers...
4
7185
by: meganrobertson22 | last post by:
Hi Everyone- I have a question about how to add and then use the "All" selection in a combo box. I am trying to figure out how to: (1) add "All" as a selection to a combo box and then (2) how to use the selection "All" as criteria for a field in a query, which is used to generate data for a report.
4
39170
by: Chris | last post by:
Hello, I am attempting to build a MS SQL query that will return data from "today"; today being current day 8:00AM-10:00PM today. My goal is to return the data from a table that is written to throughout the day, the query will provide the current grade of service in our call center. I am having difficulty defining my where clause: - I can accomplish my goal my statically defining my 'date between' as
19
5037
by: MDC | last post by:
Why does this return true: IsDate("ISometimesHateProgrammingMarch2005") Is there another way to verify that this is not a date?? Thanks in advance! MDC
2
4913
by: jerry.ranch | last post by:
I've been using row source with the QBE for my list and combo boxes..when would I use control source? jerry
0
4747
by: lgalumbres | last post by:
Hello, I have a DataGridView control with a DataGridViewComboBoxColumn that allows users to choose selections from a list. The DataGridView is bound to a DataTable object and the DataGridViewComboBoxColumn is bound to a DataTable as well and the link between the two tables is by a field called "ClassID" which is of integer data type. Both DataTables are populated from a server call. With the DataGridViewComboBoxColumn a user is...
2
3728
brettl
by: brettl | last post by:
Hey all. I'm not sure if this question should be posted here or in the XML forums. Please forgive me if its in the wrong place or feel free to move it. Any who, I'm using a SOAP service to display a list of news articles. I'm outputing the results using XSLT. I'm using the following code to strip the returned pubdate and then calculate the time between today's date and the pubdate. <xsl:template match="f">
0
8817
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
8715
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
9322
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...
1
9093
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
9039
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
7966
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
5964
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4734
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.