DB2与Oracle命令区别

由于现在数据集市项目中将即席查询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 一样;

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值