Oracle Two time subtraction

Source: Internet
Author: User
Tags date1

Oracle Two time subtraction default is the number of days

Oracle two time subtraction The default is the number of days *24 is the difference between the hours

Oracle two time subtraction The default is the number of minutes *24*60 is the difference between

Oracle two time subtraction default is the number of days *24*60*60 is the difference in seconds

--months_between (DATE2,DATE1)
Give the month of date2-date1
sql> Select months_between ('1 September-December -1999','1 September-March-1999 ') mon_between from dual;

Mon_between
-----------
9
Sql>

Select months_between (to_date ('2000.05.20','yyyy.mm.dd '), to_date ('2005.05.20','yyyy.dd ') from



Mon_betw
---------
-60

Oracle COMPUTE time-Difference expressions

--Get a two-time difference in number of seconds

SelectCeil ((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'))*  - *  - *  - *  +) difference in number of seconds fromDUAL;


/*
Number of seconds of difference
----------
86401000
1 row selected
*/

--Get the difference in seconds between two times

SelectCeil ((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'))*  - *  - *  -) difference in number of seconds fromDUAL;


/*
difference in seconds
----------
     86401
1 Row selected
*/

--Get the difference minutes between two times

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  


/*
Minutes of difference
----------
1441
1 row selected
*/

--Get the time difference between two hours

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  


/*
Hours of difference
----------
25
1 row selected
*/

--Get two time difference days

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


/*
difference in days
----------
2
1 row selected
*/

----------------------------------------
Note: The number of days can be reduced directly by 2 dates, which makes it more convenient
----------------------------------------

--Get two-time month difference

Select(EXTRACT ( Year  fromTo_date ('2009-05-01','YYYY-MM-DD'))-EXTRACT ( Year  fromTo_date ('2008-04-30','YYYY-MM-DD')))*  A +EXTRACT (Month  fromTo_date ('2008-05-01','YYYY-MM-DD'))-EXTRACT (Month  fromTo_date ('2008-04-30','YYYY-MM-DD')) months fromDual


/*
MONTHS
----------
       
1 Row selected
*/

--------------------------------------
Note: You can use the Months_between function for a more convenient
--------------------------------- -----

-Gets the two-time year difference
 

Select EXTRACT ( year from to_date ('2009-05-01','yyyy-mm-dd  '- EXTRACT ( year from to_date ('2008-04-30') ,'yyyy-mm-dd' from dual;


/*
Years
----------
1

SelectSysdate,add_months (Sysdate, A) fromDual--plus 1 yearsSelectSysdate,add_months (Sysdate,1) fromDual--Add JanuarySelectSysdate,to_char (sysdate+7,'YYYY-MM-DD HH24:MI:SS') fromDual--plus 1 weeks .SelectSysdate,to_char (sysdate+1,'YYYY-MM-DD HH24:MI:SS') fromDual--plus 1 daysSelectSysdate,to_char (sysdate+1/ -,'YYYY-MM-DD HH24:MI:SS') fromDual--plus 1 hoursSelectSysdate,to_char (sysdate+1/ -/ -,'YYYY-MM-DD HH23:MI:SS') fromDual--plus 1 minutes .SelectSysdate,to_char (sysdate+1/ -/ -/ -,'YYYY-MM-DD HH23:MI:SS') fromDual--plus 1 seconds .SelectSysdate+7    fromDual--plus 7 days

Oracle Two time subtraction

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.