您的位置:首页 > 数据库 > Oracle

DB2与Oracle命令区别

2013-05-09 12:59 309 查看
由于现在数据集市项目中将即席查询ORACLE版本的迁移DB2版本,故在网上找了下资料,也结合自己在测试过程中发现的问题,列举出DB2与Oracle命令区别。

DB2与Oracle命令区别:

1.取前N条记录

Oracle : select * from tablename where rownum <= n;

DB2:select * from tablename fetch first n rows only;

2.取得系统日期

Oracel:select sysdate from dual;

DB2: select current timestamp from sysibm.sysdummy1;

3.空置转换

Oracle:select productid,loginname,nvl(cur_rate,'0') from tablename;

DB2:select productid,loginname,value(cur_rate,'0') from tablename;

4.类型转换

Oracle: 数据类型转换函数:to_char(),to_data(),to_number()等

如果仅仅取年,月,日等,可以用to_char(sysdate,'YYYY-MM-DD'),to_char(sysdate,'YYYY'),to_char(sysdate,'MM');

DB2: 数据类型转换函数:char(),varche(),int(),data(),time()等

取得年,月,日等写法:year(current timestamp),month(current timestamp),day(current timestamp),

hour(current timestamp),minute(current timestamp),second(current timestamp),microsecond(current timestamp)

只取年月日可以用date(current timestamp),取时分秒 time(current timestamp)

char()是定长字符串1-255 varchar()是非定长字符串1-36272;

5.快速清空大表

Oracle:truncate tablename;

DB2:alter table tablename active not logged initially with empty table;

6.to_number

Oracle:select to_number('123') form dual;

DB2:select cast('123' as integer)from sysibm,sysdummy1;

select cast(current time as char(8)) from sysibm,sysdummy1;



7.创建类似表

Oracle:create table a as select * from b;

DB2:crate table a like b;

8.子查询

Oracle:直接使用子查询

DB2:With 语句

with a1 as(

select max(id) as aal from test)

select id,aal from test,al

9.数据类型

比较大的差别:

Oracle:char-2000 date datetime

DB2:char-254 date time timestamp

10.数据类型转换函数

Oracle:

整型转字符型,字符串转整型,

to_char(1),to_char(1.1),to_number('1'),to_number('1.1')

to_data('2011-04-10 19:17:30','YYYY-MM-DD HH24:MI:SS')

to_char(to_date('2011-04-10','yyyy-mm-dd'),'yyyy-mm-dd')

DB2:

char(1),int('1'),double('1.1'),char(1.1)date('2011-04-20')

to_date('2011-04-20 19:28:30','YYYY-MM-DD HH24:MI:SS')

char(date('2011-04-20'))

兼容写法

cast (1 as char)

cast ('1' as int)

11.where 条件弱类型判断

Oracle:where 字符型字段 in(整型) 是允许的

DB2:不允许

Oracle:where 字符型字段 = 数字类型字段 是允许的

DB2:不允许

12.replace 关键字

Oracle 支持,DB2不支持,create or replace 语句在DB2中非法。

13.substr不同

Oracle

select * from tablename where substr('10086',1,10)='100' with ur; 允许

select * from tablename where substr('usr_nbr',1,10)='100' with ur; 允许,不报错,但无结果集。

DB2

select * from tablename where substr('10086',1,10)='100' with ur; 不允许,报错“ SUBSTR 函数的第二个或第三个自变量超出范围”

select * from tablename where substr('usr_nbr',1,10)='100' with ur; 不允许,报错“ SUBSTR 函数的第二个或第三个自变量超出范围”

如果要从第一位取到最后一位,可以不需要第三位参数:

select substr('usr_nbr',1) from tablename;ORACLE和DB2 一样;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: