MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Selecting rows based on DB2 DATE & TIME ranges

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
Jose
Beginner


Joined: 06 May 2004
Posts: 3
Topics: 1

PostPosted: Mon Oct 20, 2008 9:06 am    Post subject: Selecting rows based on DB2 DATE & TIME ranges Reply with quote

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
View user's profile Send private message
Dibakar
Advanced


Joined: 02 Dec 2002
Posts: 700
Topics: 63
Location: USA

PostPosted: Mon Oct 20, 2008 10:03 am    Post subject: Reply with quote

Jose,

I didn't understand what you want to do, can you give example?

Diba.
Back to top
View user's profile Send private message Send e-mail
CraigG
Intermediate


Joined: 02 May 2007
Posts: 202
Topics: 0
Location: Viginia, USA

PostPosted: Mon Oct 20, 2008 10:09 am    Post subject: Reply with quote

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
View user's profile Send private message
danm
Intermediate


Joined: 29 Jun 2004
Posts: 170
Topics: 73

PostPosted: Mon Oct 20, 2008 10:14 am    Post subject: Reply with quote

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
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12382
Topics: 75
Location: San Jose

PostPosted: Mon Oct 20, 2008 10:26 am    Post subject: Reply with quote

Danm sql is an excellent choice to get the desired results between 2 date time ranges

Kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
bauer
Intermediate


Joined: 10 Oct 2003
Posts: 317
Topics: 50
Location: Germany

PostPosted: Mon Oct 20, 2008 10:46 am    Post subject: Reply with quote

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
View user's profile Send private message
Jose
Beginner


Joined: 06 May 2004
Posts: 3
Topics: 1

PostPosted: Mon Oct 20, 2008 11:17 am    Post subject: Reply with quote

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
View user's profile Send private message
Jose
Beginner


Joined: 06 May 2004
Posts: 3
Topics: 1

PostPosted: Mon Oct 20, 2008 11:30 am    Post subject: Reply with quote

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
View user's profile Send private message
haatvedt
Beginner


Joined: 14 Nov 2003
Posts: 66
Topics: 0
Location: St Cloud, Minnesota USA

PostPosted: Fri Nov 07, 2008 10:26 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group