Oracle SQL Compute time-difference expression

Source: Internet
Author: User

--取第几天的数据。 select from table where trunc(sysdate)-trunc(cr)=1;  select from table where trunc(sysdate)-trunc(cr)=2;  select from table where trunc(sysdate)-trunc(cr)=8;  --获取两时间的相差豪秒数 select ceil((To_date( ‘2008-05-02 00:00:00‘ ‘yyyy-mm-dd hh24-mi-ss‘ ) - To_date( ‘2008-04-30 23:59:59‘ ‘yyyy-mm-dd hh24-mi-ss‘ )) * 24 * 60 * 60 * 1000) 相差豪秒数  FROM DUAL; /* 相差豪秒数 ----------    86401000 1 row selected */ --获取两时间的相差秒数 select ceil((To_date( ‘2008-05-02 00:00:00‘ ‘yyyy-mm-dd hh24-mi-ss‘ ) - To_date( ‘2008-04-30 23:59:59‘ ‘yyyy-mm-dd hh24-mi-ss‘ )) * 24 * 60 * 60) 相差秒数  FROM DUAL; /* 相差秒数 ----------       86401 1 row selected */ --获取两时间的相差分钟数 select ceil(((To_date( ‘2008-05-02 00:00:00‘ ‘yyyy-mm-dd hh24-mi-ss‘ ) - To_date( ‘2008-04-30 23:59:59‘ ‘yyyy-mm-dd hh24-mi-ss‘ ))) * 24 * 60)  相差分钟数  FROM DUAL; /* 相差分钟数 ----------        1441 1 row selected */ --获取两时间的相差小时数 select ceil((To_date( ‘2008-05-02 00:00:00‘ ‘yyyy-mm-dd hh24-mi-ss‘ ) - To_date( ‘2008-04-30 23:59:59‘ ‘yyyy-mm-dd hh24-mi-ss‘ )) * 24)  相差小时数  FROM DUAL; /* 相差小时数 ----------          25 1 row selected */ --获取两时间的相差天数 select ceil((To_date( ‘2008-05-02 00:00:00‘ ‘yyyy-mm-dd hh24-mi-ss‘ ) - To_date( ‘2008-04-30 23:59:59‘ ‘yyyy-mm-dd hh24-mi-ss‘ )))  相差天数  FROM DUAL; /* 相差天数 ----------           2 1 row selected */ --获取两时间月份差 select (EXTRACT( year FROM to_date( ‘2009-05-01‘ , ‘yyyy-mm-dd‘ )) - EXTRACT( year FROM to_date( ‘2008-04-30‘ , ‘yyyy-mm-dd‘ ))) * 12 +          EXTRACT( month FROM to_date( ‘2008-05-01‘ , ‘yyyy-mm-dd‘ )) - EXTRACT( month FROM to_date( ‘2008-04-30‘ , ‘yyyy-mm-dd‘ )) months from dual; /* MONTHS ----------          13 1 row selected */ --获取两时间年份差 select EXTRACT( year FROM to_date( ‘2009-05-01‘ , ‘yyyy-mm-dd‘ )) - EXTRACT( year FROM to_date( ‘2008-04-30‘ , ‘yyyy-mm-dd‘ )) years  from dual; /* YEARS ----------           1 1 row selected */

Oracle SQL Compute time-difference expression

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.