Time Processing and time period Statistics in Oracle

Source: Internet
Author: User

Oracle time addition and subtraction

Recently, due to the time-frame addition and subtraction tests, I found some time addition and subtraction algorithms on the Internet, which can solve the pressing problem and help more Oracle beginners!

Addition
Select sysdate, add_months (sysdate, 12) from dual; -- add 1 year
Select sysdate, add_months (sysdate, 1) from dual; -- add January
Select sysdate, to_char (sysdate + 7, 'yyyy-mm-dd HH24: MI: ss') from dual; -- add 1 week
Select sysdate, to_char (sysdate + 1, 'yyyy-mm-dd HH24: MI: ss') from dual; -- add 1 day
Select sysdate, to_char (sysdate + 1/24, 'yyyy-mm-dd HH24: MI: ss') from dual; -- add 1 hour
Select sysdate, to_char (sysdate + 1/24/60, 'yyyy-mm-dd HH24: MI: ss') from dual; -- add 1 minute
Select sysdate, to_char (sysdate + 1/24/60/60, 'yyyy-mm-dd HH24: MI: ss') from dual; -- add 1 second


Subtraction
Select sysdate, add_months (sysdate,-12) from dual; -- minus 1 year
Select sysdate, add_months (sysdate,-1) from dual; -- minus January
Select sysdate, to_char (sysdate-7, 'yyyy-mm-dd HH24: MI: ss') from dual; -- minus 1 week
Select sysdate, to_char (sysdate-1, 'yyyy-mm-dd HH24: MI: ss') from dual; -- minus 1 day
Select sysdate, to_char (sysdate-1/24, 'yyyy-mm-dd HH24: MI: ss') from dual; -- minus 1 hour
Select sysdate, to_char (sysdate-1/24/60, 'yyyy-mm-dd HH24: MI: ss') from dual; -- minus 1 minute

Select sysdate, to_char (sysdate-1/24/60/60, 'yyyy-mm-dd HH24: MI: ss') from dual; -- minus 1 second

 


Statistics by time period: note that you must change the SQL statement based on the actual type in Oracle:

Code Source:

Select to_char (trunc (sysdate) + trunc (dtime-trunc (sysdate) * 24*60/30) * 30/60/24), 'hh24: mi ') | '-' | to_char (trunc (sysdate) + trunc (dtime-trunc (sysdate) * 24*60/30 + 1) * 30/60/24 ), 'hh24: mi ') period, avg (v1), sum (v2), count (*) "ROWS" from test where trunc (dtime) = to_date (& day, 'yyyymmdd') group by to_char (trunc (sysdate) + trunc (dtime-trunc (sysdate) * 24*60/30) * 30/60/24), 'hh24: mi ') |'-'| to_char (trunc (sysdate) + trunc (dtime-trunc (sysdate) * 24*60/30 + 1) * 30/60/24), 'hh24: mi ') order by 1;

 

 

My modifications:

 


Select to_char (trunc (sysdate) +
Trunc (to_date (to_char (passvehicleinfo. passtime,
'Yyyymmdhh24: mi: ss '),
'Yyyymmdd hh24: mi: ss')-trunc (sysdate) * 24*60/30) * 30/60/24 ),
'Hh24: Mi') | '-' | to_char (trunc (sysdate) + trunc (to_date (to_char (passvehicleinfo. passtime, 'yyyymmdd hh24: mi: ss'), 'yyyymmdd hh24: mi: ss')-trunc (sysdate) * 24*60/30 + 1) * 30/60/24 ), 'hh24: Mi ')


From passvehicleinfo
Group by to_char (trunc (sysdate) +
Trunc (to_date (to_char (passvehicleinfo. passtime,
'Yyyymmdhh24: mi: ss '),
'Yyyymmdd hh24: mi: ss')-trunc (sysdate) * 24*60/30) * 30/60/24 ),
'Hh24: Mi') | '-' | to_char (trunc (sysdate) + trunc (to_date (to_char (passvehicleinfo. passtime, 'yyyymmdd hh24: mi: ss'), 'yyyymmdd hh24: mi: ss')-trunc (sysdate) * 24*60/30 + 1) * 30/60/24 ), 'hh24: Mi ')

For more information about Oracle, see Oracle topics page http://www.bkjia.com/topicnews.aspx? Tid = 12

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.