Comparison of ORACLE date and timestamp data types (i)

Source: Internet
Author: User
Tags comparison date1 time interval

Date data type

This data type is so familiar that we think of date types when we need to represent dates and times. It can store months, years, days, centuries, hours, minutes, and seconds. It is typically used to indicate when something has happened or is going to happen. The problem with the date data type is that it indicates that the measure granularity of the time interval for two events is seconds. This problem will be solved when the article discusses timestamp later. You can use the To_char function to wrap date data in a traditional way to represent multiple formats.

Sql> Select To_char (date1, ' mm/dd/yyyy HH24:MI:SS ') "Date" from date_table;

Date

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

06/20/2003 16:55:14

06/26/2003 11:16:36

The trouble with most of the people I've met is to calculate the number of years, months, days, hours, and seconds between two time intervals. What you need to know is that when you subtract two dates, you get the number of days. You need to multiply the number of seconds per day (1 days = 86,400 seconds), and then you can calculate the number of intervals you want. The following is my solution, which can be used to accurately calculate the interval between two times. I know this example can be a little bit shorter, but I'm trying to show all the numbers to emphasize the way I calculate.

1 SELECT to_char (date1, ' MMDDYYYY:HH24:MI:SS ') date1,

2 To_char (date2, ' MMDDYYYY:HH24:MI:SS ') Date2,

3 Trunc (86400* (date2-date1))-

4 60* (Trunc (86400* (date2-date1))/60) seconds,

5 Trunc ((86400* (date2-date1))/60)-

6 60* (Trunc (86400* (date2-date1)/60)/60)) minutes,

7 Trunc ((86400* (date2-date1))/60/60)-

8 24* (Trunc ((86400* (date2-date1)/60)/60)/24) hours,

9 Trunc ((86400* (date2-date1)/60)/60) days,

Trunc (((86400* (date2-date1)/60)/60)/24)/7 weeks

11* from Date_table

DATE1 DATE2 SECONDS MINUTES HOURS days WEEKS

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

06202003:16:55:14 07082003:11:22:57 43 27 18 17 2

06262003:11:16:36 07082003:11:22:57 21 6 0 12 1

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.