Calculating Time Difference in Oracle

Source: Internet
Author: User

Computing time difference is a common problem of ORACLE data types. Oracle supports date calculation. You can create expressions such as "date 1-date 2" to calculate the time difference between the two dates.

Once you find the time difference, you can use a simple technique to calculate the time difference by day, hour, minute, or second. To get the data difference, you must select an appropriate time measurement unit to hide the data format.

It is tempting to use sophisticated conversion functions to convert dates, but you will find that this is not the best solution.
Round (to_number (end-date-start_date)-elapsed time (in days)

Round (to_number (end-date-start_date) * 24)-elapsed time (in hours)

Round (to_number (end-date-start_date) * 1440)-elapsed time (in minutes) k7zr {-: W [this data comes from Guizhou Learning Network http://www.gzu521.com] k7zr {-: W
What is the default display time difference mode? To find the answer to this question, let's perform a simple SQL * Plus query.
SQL> select sysdate-(sysdate-3) from dual;

Sysdate-(sysdate-3)
-------------------
3
Here we can see that Oracle uses days as the unit of the elapsed time, so we can easily use the Conversion Function to convert it to hours or minutes. However, when the number of minutes is not an integer, we will encounter the problem of placing the decimal point.
Select
(Sysdate-(sysdate-3.111) * 1440
From
Dual;

(Sysdate-(sysdate-3.111) * 1440
------------------------------
4479.83333
Of course, we can use the round function (the entire function) to solve this problem, but remember that we must first convert the date data type to the number data type.
Select
Round (to_number (sysdate-(sysdate-3.111) * 1440)
From
Dual;

round (to_number (sysdate-(sysdate-3.111) * 1440)
------------------------------------------
4480
we can use these functions to convert a elapsed time to minutes and write the value to the Oracle table. In this example, we have an offline (logoff) system-level trigger mechanism to calculate the started session time and put it into an oracle statspack user_log extension table.
Update
perfstat. stats $ user_log
set
elapsed_minutes =
round (to_number (logoff_time-logon_time) * 1440)
where
User = user_id
and
elapsed_minutes is null;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.