Solved

Time Difference

Posted on 2008-06-19
2
192 Views
Last Modified: 2010-04-06
Hi
i need to display the duration of the time from two columns.each column consists of date and time.
so in third column i need to display time difference between two columns.

i am using Asp,db2

Thank you
0
Comment
Question by:tracy_p
  • 2
2 Comments
 
LVL 18

Expert Comment

by:daveslash
ID: 21826537

Since you say that the columns contain both data and time, I'll assume they're using the datatype TIMESTAMP.

In that case, use the TIMESTAMPDIFF function.

Here's the defininition / documentation of that function.

HTH,
DaveSlash

The TIMESTAMPDIFF function returns an estimated number of intervals of the type defined by the first argument, based on the difference between two timestamps. numeric-expression The first argument must be a built-in data type of either INTEGER or SMALLINT. Valid values of interval (the first argument) are:
 
1 - Fractions of a second
2 - Seconds
4 - Minutes
8 - Hours
16 - Days
32 - Weeks
64 - Months
128 - Quarters
256 - Years 
 
string-expression
 string-expression is the result of subtracting two timestamps and converting the result to a string of length 22. The argument must be an expression that returns a value of a built-in character string or a graphic string. If string-expression is a character or graphic string, it must not be a CLOB or DBCLOB. 
 
The result of the function is an integer. If either argument can be null, the result can be null; if either argument is null, the result is the null value. 
 
The following assumptions may be used in estimating the difference: 
- there are 365 days in a year 
- there are 30 days in a month 
- there are 24 hours in a day 
- there are 60 minutes in an hour 
- there are 60 seconds in a minute
 
These assumptions are used when converting the information in the second argument, which is a timestamp duration, to the interval type specified in the first argument. The returned estimate may vary by a number of days. For example, if the number of days (interval 16) is requested for a difference in timestamps for '1997-03-01-00.00.00' and '1997-02-01-00.00.00', the result is 30. This is because the difference between the timestamps is 1 month so the assumption of 30 days in a month applies. 
 
Example - Estimate the age of employees in months.
 
SELECT
   TIMESTAMPDIFF(64,
    CAST(CURRENT_TIMESTAMP-CAST(BIRTHDATE AS TIMESTAMP) AS CHAR(22))) 
      AS AGE_IN_MONTHS 
FROM EMPLOYEE

Open in new window

0
 
LVL 18

Accepted Solution

by:
daveslash earned 500 total points
ID: 21826624

I suppose I should include the syntax of the function:

TIMESTAMPDIFF ( numeric-expression , string-expression )

0

Featured Post

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
pdf file 7 93
Automatic Image Uploading 9 91
Server error in 'XXXXXXXX' application. 12 94
find age for two dates 5 59
Using Quotation Marks in PHP This question (http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28217211.html) seems to come up a lot for developers who are new to PHP.  And it got me thinking, "How can we explain the rule…
Introduction A frequently used term in Object-Oriented design is "SOLID" which is a mnemonic acronym that covers five principles of OO design.  These principles do not stand alone; there is interplay among them.  And they are not laws, merely princ…
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question