The use of the Oracle-trunc function

Source: Internet
Author: User

Trunc function can be used to intercept date time

Usage: trunc (field name, precision)

Specific examples:

-- 按1分钟聚合 select trunc(stime,  ‘MI‘ as stime   -- 按1小时聚合 select trunc(stime,  ‘HH‘ as stime   -- 按1天聚合 select trunc(stime,  ‘DD‘ as stime   -- 示例 select trunc( cast ( ‘2017-11-09 17:42:57‘ as timestamp ),  ‘MI‘ as stime

Select Trunc (' 2017-11-09 17:42:57 ', ' MI ') as Stime Two query statement data as a result

--返回结果 2017-11-09 17:42:00 -- 按5分钟聚合 trunc(minutes_sub(stime,  minute (stime) % 5),  ‘MI‘ )   -- 按10分钟聚合 trunc(minutes_sub(stime,  minute (stime) % 10),  ‘MI‘ )   -- 示例 select trunc(minutes_sub( ‘2017-11-09 17:46:57‘ minute ( ‘2017-11-09 17:46:57‘ ) % 5),  ‘MI‘ ) --返回结果 2017-11-09 17:45:00

In table table1, there is a field named Sysdate, the row id=123, date: 2016/10/28 15:11:58

1, the interception time to the year, the SQL statement is as follows:

Select Trunc (sysdate, 'yyyy') from table1 where id=123; --YYYY can also be replaced with year

Show: 2016/1/1

2, intercept the time to the month, the SQL statement:

Select Trunc (sysdate, 'mm') from table1 where id=123;

Show: 2016/10/1

3, intercept time to day, SQL statement:

Select Trunc (sysdate, 'dd') from table1 where id=123;

Show: 2016/10/28

4, intercept the time to the hour, the SQL statement:

Select Trunc (sysdate, 'hh') from table1 where id=123;

Display: 2016/10/28 15:00:00

5, intercept the time to the minute, the SQL statement:

Select Trunc (sysdate, 'mi') from table1 where id=123;

Display: 2016/10/28 15:11:00

6, interception time to the second temporarily do not know how to operate

7, not directly with Trunc (Sysdate, ' yyyy-mm-dd '), will prompt "precision specifier too much"

The use of the Oracle-trunc 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: 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.