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

Source: Internet
Author: User
Tags date comparison difference between two times
oracle| Comparison | data | data type
Original Author: James Koopmann

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

Date

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

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

TIME1 TIME2 SECONDS MINUTES HOURS days WEEKS

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

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.

System date and Time

Returns the date data type in order to get the system time. You can use the Sysdate function.

Sql> SELECT sysdate from DUAL;

To get the system time, return to the timestamp data type. You can use the Systimpstamp function.

Sql> SELECT Systimestamp from DUAL;

You can set initialization parameters fixed_date Specify the Sysdate function to return a fixed value. This is used in the test date and time sensitive code. Note that this parameter is not valid for the Systimestamp function.

sql> ALTER SYSTEM SET fixed_date = ' 2003-01-01-10:00:00 ';

System altered.



Sql> select Sysdate from dual;

Sysdate

---------

01-jan-03



Sql> select Systimestamp from dual;

Systimestamp

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

09-jul-03 11.05.02.519000 am-06:00

When using date and timestamp types, the choice is clear. You are free to dispose of date and timestamp types. When you try to switch to a more powerful timestamp, you need to be aware that they have a similar place, and there are different places, and it's enough to cause damage. Both in terms of simplicity and spacing size each have advantages, please choose reasonably.




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.