Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups 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 Date and time format in CICS Chandru3183 CICS 2 Sat Mar 18, 2017 12:46 pm
No new posts JCL to Set Return code based on DB2 S... vinu78 DB2 17 Mon Mar 13, 2017 9:47 pm
No new posts How to get current date -1 and curren... murali.andaluri DFSORT/ICETOOL 3 Wed Mar 08, 2017 2:09 pm
No new posts Overlay the system date Danielle.Filteau SYNCSORT 4 Fri Mar 03, 2017 1:16 am
No new posts SQL query not working in Cobol program. CuriousMainframer COBOL Programming 14 Wed Feb 22, 2017 5:56 pm


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us