Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2003
    Posts
    1,605

    Unanswered: 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

  2. #2
    Join Date
    Feb 2008
    Location
    Japan
    Posts
    3,483
    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.

  3. #3
    Join Date
    Jan 2003
    Posts
    1,605
    @tonkuma, thank you very much. I just can't believe it is so simple solution.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •