Comparison of ORACLE date and timestamp data types (II.)

Source: Internet
Author: User
Tags comparison difference between two times

The format of the timestamp data is the same as the date data. Note that the TO_CHAR function supports date and timestamp, but Trunc does not support timestamp data types. This has made it clear that using the timestamp data type is more accurate than the date data type when the difference between two times is extremely important.

If you want to display timestamp decimal seconds information, refer to the following:

1 Select To_char (time1, ' mm/dd/yyyy HH24:MI:SS:FF3 ') "Date" from date_table



06/20/2003 16:55:14:000

06/26/2003 11:16:36:000

In the example above, I'm only realistic about 3 bits after the decimal point.

It is easier to calculate the data differences between timestamp than the old date data type. When you subtract directly, see what happens. The results will be easier to understand, the first line of 17 days, 18 hours, 27 minutes and 43 seconds.

1 SELECT time1,

2 Time2,

3 substr ((time2-time1), InStr ((time2-time1), "+7,2") seconds,

4 substr ((time2-time1), InStr ((time2-time1), "+4,2") minutes,

5 substr ((time2-time1), InStr ((time2-time1), "+1,2") hours,

6 trunc (To_number (substr (time2-time1), 1,instr (Time2-time1, ')))

7 trunc (To_number (substr (time2-time1), 1,instr (Time2-time1, '))/7) weeks

8* from Date_table


-------------------------  -------------------------- ------- ------- ----- ---- -----

06/20/2003:16:55:14:000000 07/08/2003:11:22:57:000000 43 27 18 17 2

06/26/2003:11:16:36:000000 07/08/2003:11:22:57:000000 21 06 00 12 1

This means no longer needing to care about how many seconds a day is in trouble calculations. Therefore, getting the number of days, months, days, hours, minutes and seconds becomes a matter of extracting numbers with the SUBSTR function.

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