EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials DB2 Tutorial DB2 TIMESTAMPDIFF
Secondary Sidebar
DB2 Tutorial
  • DB2 Tutorial
    • What is DB2?
    • DB2? current date
    • DB2 purescale
    • DB2 backup
    • DB2 restore
    • DB2 C Express
    • DB2 Version
    • DB2? Architecture
    • DB2? Data Types
    • DB2? load
    • DB2? order by
    • DB2 date
    • DB2 NVL
    • DB2? update
    • DB2 warehouse
    • DB2 grant
    • DB2 database
    • DB2 VARCHAR
    • DB2? INSERT
    • DB2 LISTAGG
    • DB2 LIKE
    • DB2 TRUNCATE TABLE
    • DB2 LIST TABLES
    • DB2 between
    • DB2? current timestamp
    • DB2? length
    • DB2? bind
    • DB2 limit rows
    • DB2? export
    • DB2 with
    • DB2 Create Table
    • DB2 case statement
    • DB2 CAST
    • DB2 Functions
    • DB2 Date Functions
    • DB2? row_number
    • DB2 trim
    • DB2? Translate
    • DB2 UNION
    • DB2 timestamp
    • DB2? TIMESTAMPDIFF
    • DB2? replace
    • DB2 merge
    • DB2 COALESCE
    • DB2 ISNULL
    • DB2? explain
    • DB2 Join
    • DB2 alter column
    • DB2 rename column
    • DB2? Describe Table
    • DB2? rename table
    • DB2 List Databases
    • DB2 LUW
    • DB2 Query
    • DB2 GROUP BY
    • DB2 TO_DATE

DB2 TIMESTAMPDIFF

DB2 TIMESTAMPDIFF

Introduction of DB2 TIMESTAMPDIFF

DB2 TIMESTAMPDIFF is a function provided by IBM that is used to find out the number of intervals in the unit of type mentioned by the first argument that is being passed to this function. TIMESTAMPDIFF function basically calculates the difference between the two timestamp values which is nothing but time or period interval. In this article, we will study how we can make the use of TIMESTAMPDIFF function to find out the interval or difference in any format or type that we want the result to be in, its syntax and implementation with the help of certain examples.

Syntax of DB2 TIMESTAMPDIFF

The syntax of TIMESTAMPDIFF function in DB2 is as shown below –

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

TIMESTAMPDIFF (first argument, second argument);

In the above syntax, we can observe that the TIMESTAMPDIFF () function accepts two parameter values to it. The first argument can be and expression that evaluates to numeric datatype or a constant numeric value. The numeric datatype expression that is expected in the first argument can be either the built-in integer or small int datatype.

The expression of value that is mentioned in the first argument should be valid and belong from any of the below mentioned values as each value individually represents the interval type that we want our result in. The following table shows how the value of first argument is considered internally.

First argument’s expression value Type of the interval considered for output
256 Years
128 Quarters
64 Months
32 Weeks
16 Days
8 Hours
4 Minutes
2 Seconds
1 Microseconds

The above table shows all the interval types and the corresponding nature of which number should be mentioned as first argument if you want to get the output in the expected type.

The second argument will be a string expression that should return the value that is in the datatype of either a built-in CHAR type or VARCHAR datatype. It can even be a graphic string datatype provided that value should not be in LOB. In case of graphic string specified as the second argument, it is firstly converted implicitly into a character string value and the function is executed to evaluate the resultant value. The value of this expression is supposed to be the difference between the two TIMESTAMP values that are provided and that value is further converted into the character string which has a length of 22 characters. It is necessary that the string value that is specified should not have more than 6 digits to the right side of the decimal point.

In order to understand the elements and contents of specifying the string expression in the TIMESTAMPDIFF() function’s argument, refer to the below table –

The element value to be mentioned in string The valid value corresponding to the value to be specified The position of the character with respect to decimal point. If this value is negative then it is treated to be on left side.
Microseconds 000000 to 999999 1 to 6
Decimal Separator A point or period is at 0 0
Seconds 0 to 59 -2 to -1
Minutes 0 to 59 or can even be blank -4 to -3
Hours It can be blank or having 0-24 value -6 to -5
Days It can be blank or having 0-30 value -8 to -7
Months It can be blank or having 0-11 value -10 to -9
Years It can be blank or having 0-9998 value -14 to -11

The result of the TIMESTAMPDIFF() function gives the value which is an integer number which has the same sign as provided in the second argument’s string expression. In case, if we specify any of the argument’s value as NULL the function will result into the NULL value as well. The evaluation of the output value of the function for each of the possible interval type specified is done as shown in the below table –

Interval type of result evaluation of the duration value in terms of those elements
Microseconds ((seconds+(minutes*60)) * 1000000) + microseconds
Seconds 60 * (60 * (24 * ((days+(30*months)) + (365*years))) + minutes) + seconds and is an integer value
Minutes 60 * (24 * ((days+(30*months)) + (365*years))) + minutes and is an integer value
Hours 24 * ((days+(30*months)) + (365*years))  and is an integer value
Days (days+(30*months)) + (365*years) and is an integer value
Weeks ((days+(30*months))/7) + (52*years) and is an integer value
Months ((12 * years)+ months)
Years years
Quarters Calculated as (months+(12 * years))/3 and is an integer value

While evaluating the timestamp difference the following assumptions are made by the IBM in DB2 –

  • There are 365 days in a single year.
  • There are 52 weeks in a single year.
  • There are 12 months in a single year.
  • There are 24 hours in a single day.
  • There are 60 minutes in a single hour.
  • There are 60 seconds in a single minute.

Examples of DB2 TIMESTAMPDIFF

Now, let us have a look at how we can implement the TIMESTAMPDIFF() function to calculate the duration or internal that is the difference between two TIMESTAMP values. Consider two timestamp values ‘1997-03-01-00.00.00’ and ‘1997-02-01-00.00.00’ and we have to calculate the difference between the two mentioned values in terms of months. We can make the use of following statement to get the difference between the two timestamp values in months as shown below –

TIMESTAMPDIFF(16, CHAR(TIMESTAMP(‘2021-03-05-00.00.00’)- TIMESTAMP(‘2021-02-06-00.00.00’)))

The above statement returns the following output as shown in the image with the difference in the timestamp giving the duration of 27 days or 00000027000000.000000  because we mentioned 16 as the numeric character that stands for days interval calculation.

DB2 TIMESTAMPDIFF-1.1

Let us consider one more example where we will be calculating the duration between two time stamps in terms of months. Consider two time stamp values ‘2021-08-06-00.00.00’ and ‘2021-07-07-00.00.00’ and the numeric argument for the first position of the function will be specified as 64 for months.

SELECT TIMESTAMPDIFF(64, CHAR(TIMESTAMP(‘2021-08-06-00.00.00’) – TIMESTAMP(‘2021-07-07-00.00.00’)))

The output of the above query is as shown below as the day difference is 30 and so the month difference will be 0 –

DB2 TIMESTAMPDIFF-1.2

Recommended Articles

This is a guide to DB2 TIMESTAMPDIFF. Here we also discuss the introduction and syntax of TIMESTAMPDIFF function in DB2 along with examples. You may also have a look at the following articles to learn more –

  1. DB2 Interview Questions
  2. What is DB2?
  3. Data Warehouse Software
  4. Hibernate Generator
Popular Course in this category
SQL Training Program (10 Courses, 8+ Projects)
  10 Online Courses |  8 Hands-on Projects |  80+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more