Portal | Manuals | References | Downloads | Info | Programs | JCLs | Mainframe wiki | Quick Ref
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Profile Log in to check your private messages Log in
 
Query to see if one date is 1 day after another date?

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
View previous topic :: :: View next topic  
Author Message
jm_green84

New User


Joined: 13 Dec 2006
Posts: 11
Location: ATL

PostPosted: Tue Jan 16, 2007 1:26 am    Post subject: Query to see if one date is 1 day after another date?
Reply with quote

How would I write a query to see if "one date is 1 day after another date?

Title changed from "query help" to "query to see if one date is 1 day after another date?" : Priyesh.
Back to top
View user's profile Send private message

dick scherrer

Site Director


Joined: 23 Nov 2006
Posts: 19270
Location: Inside the Matrix

PostPosted: Tue Jan 16, 2007 2:03 am    Post subject:
Reply with quote

Hello,

Please post some sample data and what you'd like to have returned in your query.
Back to top
View user's profile Send private message
DavidatK

Active Member


Joined: 22 Nov 2005
Posts: 700
Location: Troy, Michigan USA

PostPosted: Tue Jan 16, 2007 3:04 am    Post subject: Re: query help
Reply with quote

Maybe you can adapt this to your need

Code:

SELECT DT.DATE1,                                                   
       DT.DATE2,                                                   
       CAST(DAY(DATE(DT.DATE1) - DATE(DT.DATE2)) AS DEC(5)) AS DAYS,
       CASE                                                         
         WHEN DAY(DATE(DT.DATE1) - DATE(DT.DATE2)) = 1             
           THEN 'Y'                                                 
         WHEN DAY(DATE(DT.DATE1) - DATE(DT.DATE2)) = -1             
           THEN 'Y'                                                 
         ELSE 'N'                                                   
       END AS ONE_DAY                                               
  FROM                                                             
      (SELECT '2006-12-12'   AS DATE1,                             
              '2006-12-13'   AS DATE2                               
         FROM SYSIBM.SYSDUMMY1                                     
       UNION ALL                                                   
       SELECT '2006-12-12'   AS DATE1,                             
              '2006-12-12'   AS DATE2                               
         FROM SYSIBM.SYSDUMMY1                                     
       UNION ALL                       
       SELECT '2006-12-13'   AS DATE1,   
              '2006-12-12'   AS DATE2     
         FROM SYSIBM.SYSDUMMY1           
       UNION ALL                         
       SELECT '2007-01-01'   AS DATE1,   
              '2006-12-31'   AS DATE2     
         FROM SYSIBM.SYSDUMMY1           
       UNION ALL                         
       SELECT '2007-01-01'   AS DATE1,   
              '2007-01-03'   AS DATE2     
         FROM SYSIBM.SYSDUMMY1           
       ) DT


Result

Code:

   +----------------------------------------------+
   |   DATE1    |   DATE2    |   DAYS   | ONE_DAY |
   +----------------------------------------------+
 1_| 2006-12-12 | 2006-12-13 |     -1   | Y       |
 2_| 2006-12-12 | 2006-12-12 |      0   | N       |
 3_| 2006-12-13 | 2006-12-12 |      1   | Y       |
 4_| 2007-01-01 | 2006-12-31 |      1   | Y       |
 5_| 2007-01-01 | 2007-01-03 |     -2   | N       |
   +----------------------------------------------+
                                                   
Back to top
View user's profile Send private message
William Thompson

Global Moderator


Joined: 18 Nov 2006
Posts: 3158
Location: Tucson AZ

PostPosted: Tue Jan 16, 2007 3:31 am    Post subject: Re: query help
Reply with quote

jm_green84 wrote:
How would I write a query to see if "one date is 1 day after another date?
Quote:
The DAYS function returns an integer representation of a date.
Assuming that the "dates" are in a suitable "date" format, subtract and compare....
Back to top
View user's profile Send private message
priyesh.agrawal

Senior Member


Joined: 28 Mar 2005
Posts: 1452
Location: Chicago, IL

PostPosted: Tue Jan 16, 2007 10:33 am    Post subject: Re: Query to see if one date is 1 day after another date?
Reply with quote

How about keeping it a little simpler... icon_rolleyes.gif
Code:
CASE                                                         
         WHEN DATE1 + 1 DAY = DATE2
           THEN 'Y'                                                 
         WHEN DATE2 + 1 DAY = DATE1
           THEN 'Y'                                                 
         ELSE 'N'                                                   
       END AS ONE_DAY 

I hope syntax is correct... I m bad at that... icon_mad.gif
Back to top
View user's profile Send private message
DavidatK

Active Member


Joined: 22 Nov 2005
Posts: 700
Location: Troy, Michigan USA

PostPosted: Tue Jan 16, 2007 9:01 pm    Post subject: Re: Query to see if one date is 1 day after another date?
Reply with quote

priyesh,

This will work. He may have to 'CAST' date1 and date2 (if declared as CHAR(10)) to DATE datatype.
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2 All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts reg query on DYNALLOC feature raghuraman123 SYNCSORT 12 Wed Jan 10, 2018 2:42 pm
No new posts query to fetch record which has only ... maxsubrat DB2 12 Mon Dec 11, 2017 5:03 pm
No new posts How to query MAXGENS of an existing P... Chuchulo Hatyzak JCL & VSAM 4 Wed Nov 22, 2017 10:23 pm
No new posts Native SQL Query kishpra DB2 1 Wed Nov 22, 2017 8:38 pm
No new posts Fetching data from JHS as per the cur... arunsoods All Other Mainframe Topics 4 Wed Nov 22, 2017 1:54 pm

Facebook
Back to Top
 
Job Vacancies | Forum Rules | Bookmarks | Subscriptions | FAQ | Polls | Contact Us