View previous topic :: View next topic |
Author |
Message |
Jose Beginner
Joined: 06 May 2004 Posts: 3 Topics: 1
|
Posted: Mon Oct 20, 2008 9:06 am Post subject: Selecting rows based on DB2 DATE & TIME ranges |
|
|
Could anyone help me in writing a query for the below requirements?
I have a DB2 table with columns:
D_ACT defined as DATE (format CCYY-MM-DD)
T_ACT defined as TIME (format HH.MM.SS)
I want to retrieve all the rows of the DB2 table that meets the input select criteria of:
START-DATE & START-TIME (formats CCYY-MM-DD & HH.MM.SS)
END-DATE & END-TIME (same as above)
Thanks. |
|
Back to top |
|
 |
Dibakar Advanced

Joined: 02 Dec 2002 Posts: 700 Topics: 63 Location: USA
|
Posted: Mon Oct 20, 2008 10:03 am Post subject: |
|
|
Jose,
I didn't understand what you want to do, can you give example?
Diba. |
|
Back to top |
|
 |
CraigG Intermediate
Joined: 02 May 2007 Posts: 202 Topics: 0 Location: Viginia, USA
|
Posted: Mon Oct 20, 2008 10:09 am Post subject: |
|
|
Without more details as to what your selection criteria would look like it is hard to offer any help. A timestamp column would make it simpler but with the date and time separated it can result in complex where statements.
To select the rows with a date & time greater than or equal to 2008-10-01 at 11:00:00 and a date & time less than of equal to 2008-10-03 at 13:00:00
Code: | where ((d_act = '2008-10-01' and t_act >= '11:00:00')
or d_act > '2008-10-01')
and ((d_act = '2008-10-03' and t-act <= '13:00:00')
or d_act < '2008-10-03')
|
|
|
Back to top |
|
 |
danm Intermediate
Joined: 29 Jun 2004 Posts: 170 Topics: 73
|
Posted: Mon Oct 20, 2008 10:14 am Post subject: |
|
|
Try Code: |
SELECT * FROM TABLE
WHERE CHAR(D_ACT) || CHAR(T_ACT)
BETWEEN 'CCYY-MM-DDHH.MM.SS' AND 'CCYY-MM-DDHH.MM.SS' |
|
|
Back to top |
|
 |
kolusu Site Admin

Joined: 26 Nov 2002 Posts: 12382 Topics: 75 Location: San Jose
|
Posted: Mon Oct 20, 2008 10:26 am Post subject: |
|
|
Danm sql is an excellent choice to get the desired results between 2 date time ranges
Kolusu |
|
Back to top |
|
 |
bauer Intermediate
Joined: 10 Oct 2003 Posts: 317 Topics: 50 Location: Germany
|
Posted: Mon Oct 20, 2008 10:46 am Post subject: |
|
|
Well, i agree with kolusu.
But i'm not sure: Is an index on the date and time column used for this select?
For performance critical applications the soloution of CraigG may be better.
regards
bauer |
|
Back to top |
|
 |
Jose Beginner
Joined: 06 May 2004 Posts: 3 Topics: 1
|
Posted: Mon Oct 20, 2008 11:17 am Post subject: |
|
|
danm, your query is much simpler than mine. Thanks to you and to all who provided solutions & suggestions.
I tested my own query afew times and it looks like it is working (not 100% sure) but yours is so much simpler. My query looks like this:
SELECT * FROM TABLE
WHERE
(D_ACT = :START-DATE
AND T_ACT >= :START-TIME
AND
( (D_ACT = :END-DATE
AND T_ACT <= :END-TIME)
OR (D_ACT < :END-DATE) )
)
OR
( D_ACT > :START-DATE
AND D_ACT = :END-DATE
AND T_ACT <= :END-TIME)
OR
( D_ACT > :START-DATE
AND D_ACT < :END-DATE) |
|
Back to top |
|
 |
Jose Beginner
Joined: 06 May 2004 Posts: 3 Topics: 1
|
Posted: Mon Oct 20, 2008 11:30 am Post subject: |
|
|
bauer, The exisiting DB2 table is not indexed by these columns, D_ACT & T_ACT. That's one of my concerns for performance issue. I will try CraigG's suggestion as well. Thanks. |
|
Back to top |
|
 |
haatvedt Beginner
Joined: 14 Nov 2003 Posts: 66 Topics: 0 Location: St Cloud, Minnesota USA
|
Posted: Fri Nov 07, 2008 10:26 pm Post subject: |
|
|
Jose,
this would be much simpler if your table had a TIMESTAMP column instead of a DATE and a TIME column.
if you use separate columns you may want to consider using the following... do EXPLAINS on both versions to check the access path.
the reason for the separate criteria on the D_ACT is to cause DB2 to use an index which has the D_ACT as the first column. I would suggest adding T_ACT as the second column on the index.
SELECT * FROM TABLE
WHERE D_ACT BETWEEN :START-DATE AND :END-DATE
AND
(D_ACT = :START-DATE
AND T_ACT >= :START-TIME
AND
( (D_ACT = :END-DATE
AND T_ACT <= :END-TIME)
OR (D_ACT < :END-DATE) )
)
OR
( D_ACT > :START-DATE
AND D_ACT = :END-DATE
AND T_ACT <= :END-TIME)
OR
( D_ACT > :START-DATE
AND D_ACT < :END-DATE) _________________ Chuck Haatvedt
email --> clastnameatcharterdotnet
(replace lastname, at, dot with appropriate
characters) |
|
Back to top |
|
 |
|
|