oracle和db2的sql语句区别

2010-08-26 10:40:11| 分类: 技术知识 | 标签: |字号大中小 订阅

1、取前N条记录

Oracle:Select * from TableName where rownum <= N;

DB2:Select * from TableName fetch first N rows only;

2、取得系统日期

Oracle: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;

Coalesce(cur_rate,'0')

4、类型转换(8版有了to_char,to_date,9版新增了to_number)

Oracle:select to_char(sysdate,'YYYY-MM-DD HH24:MI:SS') from dual;

DB2:select varchar(current timestamp) from sysibm.sysdummy1;

l Oracle数据类型改变函数:to_char()、to_date()、to_number()等;如果仅仅取年,月,日等,可以用to_char(sysdate, 'YYYY'),to_char('MM') ,to_char('DD')取得。只取年月日TRUNC(SYSDATE),取时分秒TO_CHAR(SYSDATE,'HH24:MI:SS')。

l DB2数据类型改变函数:char()、varchar()、int()、date()、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-32672)日期,时间形态变为字符形态: char(current date),char(current time)将字符串转换成日期或时间形态:TIMESTAMP('2002-10-2012:00:00'),DATE('2002-10-20'),DATE('10/20/2002'),TIME('12:00:00')

l 目前DB2 V8也支持to_char和to_date

5、快速清空大表

Oracle:truncate table TableName ;

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

6、关于ROWID

Oracle它是由数据库唯一产生的,在程序里可以获得DB2 v8也有此功能。

7、To_Number

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

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

SELECT CAST ( current time as char(8)) FROMsysibm.sysdummy1

8、创建类似表

Oracle:create table a as select * from b ;

DB2:create table a like b ;

CREATE TABLE tab_newAS select col1,col2…FROMtab_old DEFINITION ONLY (8版有效,9版无效)

9、decode方法

Oracle:decode方法(DECODE(条件,值1,翻译值1,值2,翻译值2,...值n,翻译值n,缺省值))或者case语句DB2中只有CASE表达式SELECT id ,name ,CASE WHEN integer(flag)=0 THEN ‘假’ WHEN integer(flag)=1 THEN ‘真’ ELSE ‘异常’END FROM TEST或者SELECT id ,name , CASE integer(flag) WHEN 0 THEN ‘假’ WHEN 1 THEN ‘真’ELSE ‘异常’END FROM TEST

10、子查询(8版,9版也支持子查询)

Oracle:直接用子查询

Db2:with语句WITH a1 AS(select max(id) as aa1 from test ) select id ,aa1 from test ,a1

11、数据类型

比较大的差别:

Oracle:char 2000

DB2: char 254

Oracle: date datetime

Db2: DATE:日期TIME:时间TIMESTAMP:日期时间