IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Query to see if one date is 1 day after another date?


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
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
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

Moderator Emeritus


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

PostPosted: Tue Jan 16, 2007 2:03 am
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
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: 3156
Location: Tucson AZ

PostPosted: Tue Jan 16, 2007 3:31 am
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: 1448
Location: Chicago, IL

PostPosted: Tue Jan 16, 2007 10:33 am
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
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 View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts Replacing 'YYMMDD' with date, varying... SYNCSORT 3
No new posts Modifying Date Format Using DFSORT DFSORT/ICETOOL 9
No new posts RC query -Time column CA Products 3
No new posts Need to convert date format DFSORT/ICETOOL 20
No new posts Dynamically pass table name to a sele... DB2 2
Search our Forums:

Back to Top