--Start
We all know that numbers can be added, minus, multiply, divide, and so on. So, could the date be? The answer is that the date can only be added, minus operations.
Before starting the operation date, let's look at what date data types are supported by Oracle, as follows:
[SQL]View PlainCopy
- DATE
- TIMESTAMP
- TIMESTAMP with time ZONE
- TIMESTAMP with LOCAL time ZONE
- INTERVAL Day to SECOND
- INTERVAL year to MONTH
Next, let's look at a simple example, as follows:
[SQL]View PlainCopy
- Select Sysdate + INTERVAL ' 1 ' Day , Sysdate-interval ' 2 ' Day from dual;
- Select Systimestamp + INTERVAL ' 1 ' Day , Systimestamp-interval ' 2 ' Day from dual;
As shown above, we can calculate the date by adding a time interval to the date, and here are more examples.
[SQL]View PlainCopy
- Date + 1 sysdate + INTERVAL ' 1 ' year
- Date + January Sysdate + INTERVAL ' 1 ' MONTH
- Date + 1st Sysdate + INTERVAL ' 1 ' Day
- Date + 1 o'clock sysdate + INTERVAL ' 1 ' HOUR
- Date + 1 minutes sysdate + INTERVAL ' 1 ' MINUTE
- Date + 1 seconds sysdate + INTERVAL ' 1 ' SECOND
- Date + 1st 1:1 sysdate + INTERVAL ' 1 1:1 ' Day to MINUTE
If the DATE type is calculated, we can also use the following method.
[SQL]View PlainCopy
- date+ 1st Sysdate + 1
- Date+ 1 O'Clock Sysdate + 1/(1*24)
- date+ 1 min sysdate + 1/(1*24*60)
- Date+ 1 sec sysdate + 1/(1*24*60*60)
- date+ 1st 1:1 sysdate + 1 + 1/(1*24) + 1/(1*24*60)
Sometimes we need to know how many days apart between two dates, that is, the time interval between dates, what should we do? It is natural for us to think of subtracting the two dates as follows:
[SQL]View PlainCopy
- Select (date ' 2015-04-10 '- date ' 2014-03-11 ') from dual
- Select (date ' 2015-04-10 '- date ' 2014-03-11 ') year to MONTH from dual
- Select (date ' 2015-04-10 '- date ' 2014-03-11 ') Day (3) to SECOND from dual
- Select (timestamp ' 2015-04-10 14:52:19.000 '- timestamp ' 2014-03-11 13:55:29.000 ') from dual
- Select (timestamp ' 2015-04-10 14:52:19.000 '- timestamp ' 2014-03-11 13:55:29.000 ') year to MONTH from dual
- Select (timestamp ' 2015-04-10 14:52:19.000 '- timestamp ' 2014-03-11 13:55:29.000 ') Day (3) To SECOND from dual
Once we have the time interval, we can extract the components of the time interval through the EXTRACT function.
[SQL]View PlainCopy
- Select EXTRACT (Year from (date ' 2015-04-10 ' – date ' 2014-03-11 ') year to MONTH) from dual
- Select EXTRACT ( date ' 2015-04-10 '- date ' 2014-03-11 ') year to MONTH) from dual
- Select EXTRACT ( date ' 2015-04-10 '- date ' 2014-03-11 ') Day (3) to SECOND) from dual
We can also pull out the following sections.
[SQL]View PlainCopy
- Year
- MONTH
- Day
- HOUR
- MINUTE
- SECOND
- Timezone_hour
- Timezone_minute
- Timezone_region
- Timezone_abbr
--See also: Oracle SQL extract
-- declaration: Reprint please indicate the source
--Last edited on 2015-06-19
--Created by Shangbo on 2015-03-10
--End
Oracle date Plus and minus operations