Home Database Mysql Tutorial Oracle中的INTERVAL数据类型详解

Oracle中的INTERVAL数据类型详解

Jun 07, 2016 pm 05:58 PM
interval oracle data type Detailed explanation

NTERVAL YEAR TO MONTH数据类型 Oracle语法: INTERVAL integer [- integer] {YEAR | MONTH} [(precision)][TO {YEAR | MONTH}] 该数据类型常用来表示一段时间差, 注意时间差只精确到年和月. precision为年或月的精确域, 有效范围是0到9, 默认值为2. eg: INTER

NTERVAL YEAR TO MONTH数据类型

Oracle语法:
INTERVAL 'integer [- integer]' {YEAR | MONTH} [(precision)][TO {YEAR | MONTH}]

该数据类型常用来表示一段时间差, 注意时间差只精确到年和月. precision为年或月的精确域, 有效范围是0到9, 默认值为2.

eg:
INTERVAL '123-2' YEAR(3) TO MONTH    
表示: 123年2个月, "YEAR(3)" 表示年的精度为3, 可见"123"刚好为3为有效数值, 如果该处YEAR(n), n
INTERVAL '123' YEAR(3)
表示: 123年0个月

INTERVAL '300' MONTH(3)
表示: 300个月, 注意该处MONTH的精度是3啊.

INTERVAL '4' YEAR    
表示: 4年, 同 INTERVAL '4-0' YEAR TO MONTH 是一样的

INTERVAL '50' MONTH    
表示: 50个月, 同 INTERVAL '4-2' YEAR TO MONTH 是一样

INTERVAL '123' YEAR    
表示: 该处表示有错误, 123精度是3了, 但系统默认是2, 所以该处应该写成 INTERVAL '123' YEAR(3) 或"3"改成大于3小于等于9的数值都可以的

INTERVAL '5-3' YEAR TO MONTH + INTERVAL '20' MONTH =
INTERVAL '6-11' YEAR TO MONTH
表示: 5年3个月 + 20个月 = 6年11个月

与该类型相关的函数:
NUMTODSINTERVAL(n, 'interval_unit')
将n转换成interval_unit所指定的值, interval_unit可以为: DAY, HOUR, MINUTE, SECOND
注意该函数不可以转换成YEAR和MONTH的.

NUMTOYMINTERVAL(n, 'interval_unit')
interval_unit可以为: YEAR, MONTH

eg: (Oracle Version 9204, RedHat Linux 9.0)
SQL> select numtodsinterval(100,'DAY') from dual;

NUMTODSINTERVAL(100,'DAY')                                                     
---------------------------------------------------------------------------    
+000000100 00:00:00.000000000                                                 

SQL> c/DAY/SECOND
  1* select numtodsinterval(100,'SECOND') from dual
SQL> /

NUMTODSINTERVAL(100,'SECOND')                                                  
---------------------------------------------------------------------------    
+000000000 00:01:40.000000000                                                 

SQL> c/SECOND/MINUTE
  1* select numtodsinterval(100,'MINUTE') from dual
SQL> /

NUMTODSINTERVAL(100,'MINUTE')                                                  
---------------------------------------------------------------------------    
+000000000 01:40:00.000000000                                                 

SQL> c/MINUTE/HOUR
  1* select numtodsinterval(100,'HOUR') from dual
SQL> /

NUMTODSINTERVAL(100,'HOUR')                                                    
---------------------------------------------------------------------------    
+000000004 04:00:00.000000000                                                 

SQL> c/HOUR/YEAR
  1* select numtodsinterval(100,'YEAR') from dual
SQL> /
select numtodsinterval(100,'YEAR') from dual
                           *
ERROR at line 1:
ORA-01760: illegal argument for function

SQL> select numtoyminterval(100,'year') from dual;

NUMTOYMINTERVAL(100,'YEAR')                                                    
---------------------------------------------------------------------------    
+000000100-00                                                                 

SQL> c/year/month
  1* select numtoyminterval(100,'month') from dual
SQL> /

NUMTOYMINTERVAL(100,'MONTH')                                                   
---------------------------------------------------------------------------    
+000000008-04                                                                 


时间的计算:
SQL> select to_date('1999-12-12','yyyy-mm-dd') - to_date('1999-12-01','yyyy-mm-dd') from dual;

TO_DATE('1999-12-12','YYYY-MM-DD')-TO_DATE('1999-12-01','YYYY-MM-DD')          
---------------------------------------------------------------------          
                                                                   11          
-- 可以相减的结果为天.

SQL> c/1999-12-12/1999-01-12
  1* select to_date('1999-01-12','yyyy-mm-dd') - to_date('1999-12-01','yyyy-mm-dd') from dual
SQL> /

TO_DATE('1999-01-12','YYYY-MM-DD')-TO_DATE('1999-12-01','YYYY-MM-DD')          
---------------------------------------------------------------------          
                                                                 -323          
-- 也可以为负数的

SQL> c/1999-01-12/2999-10-12
  1* select to_date('2999-10-12','yyyy-mm-dd') - to_date('1999-12-01','yyyy-mm-dd') from dual
SQL> /

TO_DATE('2999-10-12','YYYY-MM-DD')-TO_DATE('1999-12-01','YYYY-MM-DD')          
---------------------------------------------------------------------          
                                                               365193         

下面看看INTERVAL YEAR TO MONTH怎么用.
SQL> create table bb(a date, b date, c interval year(9) to month);

Table created.

SQL> desc bb;
Name                                      Null?    Type
----------------------------------------- -------- ----------------------------
A                                                  DATE
B                                                  DATE
C                                                  INTERVAL YEAR(9) TO MONTH

SQL> insert into bb values(to_date('1985-12-12', 'yyyy-mm-dd'), to_date('1984-12-01','yyyy-mm-dd'), null)

1 row created.

SQL> select * from bb;

A         B                                                                    
--------- ---------                                                            
C                                                                              
---------------------------------------------------------------------------    
12-DEC-85 01-DEC-84                                                            
                                                                               
                                                                               
SQL> update bb set c = numtoyminterval(a-b, 'year');

1 row updated.

SQL> select * from bb;

A         B                                                                    
--------- ---------                                                            
C                                                                              
---------------------------------------------------------------------------    
12-DEC-85 01-DEC-84                                                            
+000000376-00                                                                  
                                                                               
-- 直接将相减的天变成年了, 因为我指定变成年的
SQL> select a-b, c from bb;

       A-B                                                                     
----------                                                                     
C                                                                              
---------------------------------------------------------------------------    
       376                                                                     
+000000376-00                                                                  
                                                                              

SQL> insert into bb values(null,null,numtoyminterval(376,'month'));

1 row created.

SQL> select * from bb;

A         B             C                                                       
--------- ---------    --------------------------------------------    
12-DEC-85 01-DEC-84    +000000376-00                                                                  
                         +000000031-04                                        

SQL> insert into bb values ( null,null, numtoyminterval(999999999,'year'));

1 row created.

SQL> select * from bb;

A           B            C                                
---------   ---------     ---------------------------------------------------------------------    
12-DEC-85   01-DEC-84   +000000376-00                                                                  
                          +000000031-04
                          +999999999-00                                                               

========================

INTERVAL YEAR TO MONTH类型2个TIMESTAMP类型的时间差别。内部类型是182,长度是5。其中4个字节存储年份差异,存储的时候在差异上加了一个0X80000000的偏移量。一个字节存储月份的差异,这个差异加了60的偏移量。

SQL> ALTER TABLE TestTimeStamp ADD E INTERVAL YEAR TO MONTH;
SQL> update testTimeStamp set e=(select interval '5' year + interval '10' month year  from dual);

已更新3行。

SQL> commit;
提交完成。

SQL> select dump(e,16) from testTimeStamp;

DUMP(E,16)
---------------------------------------------
Typ=182 Len=5: 80,0,0,5,46
Typ=182 Len=5: 80,0,0,5,46
Typ=182 Len=5: 80,0,0,5,46

年:0X80000005-0X80000000=5
月:0x46-60=10

INTERVAL DAY TO SECOND数据类型

Oracle语法:
INTERVAL '{ integer | integer time_expr | time_expr }'
{ { DAY | HOUR | MINUTE } [ ( leading_precision ) ]
| SECOND [ ( leading_precision [, fractional_seconds_precision ] ) ] }
[ TO { DAY | HOUR | MINUTE | SECOND [ (fractional_seconds_precision) ] } ]

leading_precision值的范围是0到9, 默认是2. time_expr的格式为:HH[:MI[:SS[.n]]] or MI[:SS[.n]] or SS[.n], n表示微秒.
该类型与INTERVAL YEAR TO MONTH有很多相似的地方,建议先看INTERVAL YEAR TO MONTH再看该文.

范围值:
HOUR:    0 to 23
MINUTE: 0 to 59
SECOND: 0 to 59.999999999

eg:
INTERVAL '4 5:12:10.222' DAY TO SECOND(3)
表示: 4天5小时12分10.222秒

INTERVAL '4 5:12' DAY TO MINUTE
表示: 4天5小时12分

INTERVAL '400 5' DAY(3) TO HOUR
表示: 400天5小时, 400为3为精度,所以"DAY(3)", 注意默认值为2.

INTERVAL '400' DAY(3)
表示: 400天

INTERVAL '11:12:10.2222222' HOUR TO SECOND(7)
表示: 11小时12分10.2222222秒

INTERVAL '11:20' HOUR TO MINUTE
表示: 11小时20分

INTERVAL '10' HOUR
表示: 10小时

INTERVAL '10:22' MINUTE TO SECOND
表示: 10分22秒

INTERVAL '10' MINUTE
表示: 10分

INTERVAL '4' DAY
表示: 4天

INTERVAL '25' HOUR
表示: 25小时

INTERVAL '40' MINUTE
表示: 40分

INTERVAL '120' HOUR(3)
表示: 120小时

INTERVAL '30.12345' SECOND(2,4)    
表示: 30.1235秒, 因为该地方秒的后面精度设置为4, 要进行四舍五入.

INTERVAL '20' DAY - INTERVAL '240' HOUR = INTERVAL '10-0' DAY TO SECOND
表示: 20天 - 240小时 = 10天0秒

==================
该部分来源:http://www.Oraclefans.cn/forum/showblog.jsp?rootid=140
INTERVAL DAY TO SECOND类型存储两个TIMESTAMP之间的时间差异,用日期、小时、分钟、秒钟形式表示。该数据类型的内部代码是183,长度位11字节:

l         4个字节表示天数(增加0X80000000偏移量)
l         小时、分钟、秒钟各用一个字节表示(增加60偏移量)
l         4个字节表示秒钟的小时差异(增加0X80000000偏移量)

以下是一个例子:

SQL> alter table testTimeStamp add f interval day to second ;

表已更改。

SQL> update testTimeStamp set f=(select interval '5' day + interval '10' second from dual);

已更新3行。

SQL> commit;

提交完成。

SQL> select dump(f,16) from testTimeStamp;

DUMP(F,16)

--------------------------------------------------------------------------------

Typ=183 Len=11: 80,0,0,5,3c,3c,46,80,0,0,0
Typ=183 Len=11: 80,0,0,5,3c,3c,46,80,0,0,0
Typ=183 Len=11: 80,0,0,5,3c,3c,46,80,0,0,0

日期:0X80000005-0X80000000=5

小时:60-60=0
分钟:60-60=0
秒钟:70-60=10
秒钟小数部分:0X80000000-0X80000000=0
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to check tablespace size of oracle How to check tablespace size of oracle Apr 11, 2025 pm 08:15 PM

To query the Oracle tablespace size, follow the following steps: Determine the tablespace name by running the query: SELECT tablespace_name FROM dba_tablespaces; Query the tablespace size by running the query: SELECT sum(bytes) AS total_size, sum(bytes_free) AS available_space, sum(bytes) - sum(bytes_free) AS used_space FROM dba_data_files WHERE tablespace_

How to view instance name of oracle How to view instance name of oracle Apr 11, 2025 pm 08:18 PM

There are three ways to view instance names in Oracle: use the "sqlplus" and "select instance_name from v$instance;" commands on the command line. Use the "show instance_name;" command in SQL*Plus. Check environment variables (ORACLE_SID on Linux) through the operating system's Task Manager, Oracle Enterprise Manager, or through the operating system.

How to encrypt oracle view How to encrypt oracle view Apr 11, 2025 pm 08:30 PM

Oracle View Encryption allows you to encrypt data in the view, thereby enhancing the security of sensitive information. The steps include: 1) creating the master encryption key (MEk); 2) creating an encrypted view, specifying the view and MEk to be encrypted; 3) authorizing users to access the encrypted view. How encrypted views work: When a user querys for an encrypted view, Oracle uses MEk to decrypt data, ensuring that only authorized users can access readable data.

How to uninstall Oracle installation failed How to uninstall Oracle installation failed Apr 11, 2025 pm 08:24 PM

Uninstall method for Oracle installation failure: Close Oracle service, delete Oracle program files and registry keys, uninstall Oracle environment variables, and restart the computer. If the uninstall fails, you can uninstall manually using the Oracle Universal Uninstall Tool.

How to delete all data from oracle How to delete all data from oracle Apr 11, 2025 pm 08:36 PM

Deleting all data in Oracle requires the following steps: 1. Establish a connection; 2. Disable foreign key constraints; 3. Delete table data; 4. Submit transactions; 5. Enable foreign key constraints (optional). Be sure to back up the database before execution to prevent data loss.

How to solve the problem of closing oracle cursor How to solve the problem of closing oracle cursor Apr 11, 2025 pm 10:18 PM

The method to solve the Oracle cursor closure problem includes: explicitly closing the cursor using the CLOSE statement. Declare the cursor in the FOR UPDATE clause so that it automatically closes after the scope is ended. Declare the cursor in the USING clause so that it automatically closes when the associated PL/SQL variable is closed. Use exception handling to ensure that the cursor is closed in any exception situation. Use the connection pool to automatically close the cursor. Disable automatic submission and delay cursor closing.

How to set up users of oracle How to set up users of oracle Apr 11, 2025 pm 08:21 PM

To create a user in Oracle, follow these steps: Create a new user using the CREATE USER statement. Grant the necessary permissions using the GRANT statement. Optional: Use the RESOURCE statement to set the quota. Configure other options such as default roles and temporary tablespaces.

How to check invalid numbers of oracle How to check invalid numbers of oracle Apr 11, 2025 pm 08:27 PM

Oracle Invalid numeric errors may be caused by data type mismatch, numeric overflow, data conversion errors, or data corruption. Troubleshooting steps include checking data types, detecting digital overflows, checking data conversions, checking data corruption, and exploring other possible solutions such as configuring the NLS_NUMERIC_CHARACTERS parameter and enabling data verification logging.

See all articles