If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > How to get years, months and days from two dates

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-11-08, 08:31
grofaty grofaty is offline
Registered User
 
Join Date: Jan 2003
Posts: 1,570
How to get years, months and days from two dates

Hi,
I would like to get how many years, months and days is between two dates.

I have written:
Code:
db2 values days(date('2008-08-31'))-days(date('2008-01-01'))
the result is: 243 days.

How to get from this result how many years, months and days have elapsed from first to second date.
Regards,
Grofaty
Reply With Quote
  #2 (permalink)  
Old 11-11-08, 09:33
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Code:
------------------------------ Commands Entered ------------------------------
values date('2008-08-31')-date('2008-01-01');
------------------------------------------------------------------------------

1         
----------
      730.
 yyyymmdd

  1 record(s) selected.
Please note 2nd to 4th rows of the following results.
Code:
------------------------------ Commands Entered ------------------------------
SELECT to_date, from_date
     , DAYS(to_date) - DAYS(from_date) AS "days"
     , to_date - from_date             AS " yyyymmdd"
  FROM (VALUES ( DATE('2008-08-31'), DATE('2008-01-01') )
             , ( DATE('2009-03-01'), DATE('2009-02-01') )
             , ( DATE('2008-03-01'), DATE('2008-02-01') )
             , ( DATE('2008-04-01'), DATE('2008-03-01') )
       ) data(to_date, from_date);
------------------------------------------------------------------------------


TO_DATE    FROM_DATE  days         yyyymmdd 
---------- ---------- ----------- ----------
2008-08-31 2008-01-01         243       730.
2009-03-01 2009-02-01          28       100.
2008-03-01 2008-02-01          29       100.
2008-04-01 2008-03-01          31       100.
Reply With Quote
  #3 (permalink)  
Old 11-12-08, 02:34
grofaty grofaty is offline
Registered User
 
Join Date: Jan 2003
Posts: 1,570
@tonkuma, thank you very much. I just can't believe it is so simple solution.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On