oracle interval year to month数据类型-凯发app官方网站

凯发app官方网站-凯发k8官网下载客户端中心 | | 凯发app官方网站-凯发k8官网下载客户端中心
  • 博客访问: 3977419
  • 博文数量: 536
  • 博客积分: 10470
  • 博客等级: 上将
  • 技术积分: 4825
  • 用 户 组: 普通用户
  • 注册时间: 2006-05-26 14:08
文章分类

全部博文(536)

文章存档

2024年(3)

2021年(1)

2019年(1)

2017年(1)

2016年(2)

2013年(2)

2012年(10)

2011年(43)

2010年(10)

2009年(17)

2008年(121)

2007年(252)

2006年(73)

相关博文
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·

分类: oracle

2006-12-08 15:05:33

interval 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<3就会出错, 注意默认是2.

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                                                                
 
========================
今天来添加点新的东西![2008-07-26] 这部分东东来源:

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
 
阅读(5281) | 评论(2) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2009-06-07 08:57:48

不错

|
")); function link(t){ var href= $(t).attr('href'); href ="?url=" encodeuricomponent(location.href); $(t).attr('href',href); //setcookie("returnouturl", location.href, 60, "/"); }
网站地图