Some differences between Oracle and DB2 conversion

1. Take the first N records

OracleSelect * from TableName where rownum <= N;

DB2Select * from TableName fetch first N rows only;

 

2. Get the system date

OracleSelect sysdate from dual;

DB2Select current timestamp from sysibm.sysdummy1;

 

3. Null value conversion

OracleSelect productid,loginname,nvl(cur_rate,'0') from TableName ;

DB2Select productid,loginname,value(cur_rate,'0') from TableName;

Coalesce(cur_rate,'0')

 

4. Type conversion ( version 8 has to_char, to_date, and version 9 adds to_number )

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

DB2select varchar(current timestamp) from sysibm.sysdummy1;

       Oracle data type changing functions: to_char() , to_date() , to_number() , etc.; if you only take the year, month, day, etc., you can use to_char(sysdate, 'YYYY'), to_char('MM') , to_char(' DD') to obtain. Only take the year, month, and day TRUNC(SYSDATE) , and take the hour, minute, and second TO_CHAR(SYSDATE,'HH24:MI:SS') .

       DB2 data type changing functions: char() , varchar() , int() , date() , time() , etc.; get the writing of year, month, day, etc.: YEAR(current timestamp) , MONTH(current timestamp) , DAY (current timestamp) , HOUR(current timestamp) , MINUTE(current timestamp) , SECOND(current timestamp) ,  MICROSECOND(current timestamp) , you can use DATE(current timestamp) only for the year, month and day, and TIME(current timestamp) for the time, minute and second . Char() is a fixed-length string ( 1-255 ), varchar() is a non-fixed-length string ( 1-32672 ) date ,Change the time form to character form : char(current date) , char(current time) convert the string to date or time form : TIMESTAMP('2002-10-20 12:00:00'),DATE('2002-10 -20'),DATE('10/20/2002'),TIME('12:00:00')

      Currently DB2 V8 also supports to_char and to_date

 

5. Quickly clear large tables

Oracletruncate table TableName ;

DB2alter table TableName active not logged initially with empty table;

 

6. About ROWID

Oracle    It is uniquely generated by the database and can be obtained in the program

DB2        v8 also has this feature.

 

7To_Number

Oracleselect to_number('123') from dual;

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

SELECT  CAST ( current time as char(8)) FROM sysibm.sysdummy1

 

8. Create a similar table

Oraclecreate table a as select * from b ;

DB2create table a like b ;

CREATE TABLE tab_new  AS select col1,col2FROM tab_old DEFINITION ONLY ( valid for version 8, invalid for version 9 )  

 

9. decode method _

Oracledecode方法(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、数据类型

比较大的差别:

Oraclechar  2000

DB2:     char  254

 

Oracle:   date  datetime

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

 

http://hi.baidu.com/neusunly/blog/item/3867a9faaae96fdcb58f3148.html

 

1、数据类型转换函数

 

 

整型转字符型

字符串转整形

字符串转浮点型

浮点型转字符串

字符串转日期

字符串转时间戳

日期转字符串

ORACLE

to_char(1)

to_number('1')

to_number('1.1')

to_char(1.1)

to_date('2007-04-26','yyyy-mm-dd')

to_date('2007-04-26 08:08:08','YYYY-MM-DD HH24:MI:SS')

to_char(to_date('2007-04-29','yyyy-mm-dd'),'yyyy-mm-dd')

DB2

char(1)

int('1')

double('1.1')

char(1.1)

date('2007-04-26')

to_date('2007-04-26 08:08:08','YYYY-MM-DD HH24:MI:SS')

char(date('2007-04-29'))

兼容写法

cast(1 as char)

cast('1' as int)

兼容

 

2、Where条件弱类型判断
oracle: where 字符型字段 in (整形) 是允许,DB2不允许
select 'abc' from dual where '1' in (1) 在oracle下可通过
select 'abc' from sysibm.sysdummy1 where '1' in (1) 在DB2下报错

 

oracle:where 字符型字段=数字型字段 允许,DB2不允许

select 'abc' from dual where '1'=1 在oracle下可通过
select 'abc' from sysibm.sysdummy1 whre '1'=1 在DB2下报错


3、replace关键字
oracle支持,DB2不支持 create or replace语句在DB2下是非法的


4、子查询别名
ORACLE 支持select * from(select 1 from dual) 或者 select * from(select 1 from dual) t

DB2 支持select * from(select 1 from sysibm.sysdummy1) t 或者 select * from(select 1 from sysibm.sysdummy1) as t

固兼容的写法是select * from(子查询) t


5、DATE数据类型的区别
ORACLE中DATE型也是带有时分秒的,但DB2下DATE只是年月日,如'2007-04-28',且可作为字符串直接操作,DB2中要记录时分秒必须采用TIMESTAMP型

 

一个采用hibernate后常见的兼容问题是:

如果在映射文件中定义了某个字段为Date型,

则在DB2下,此字段必须定义为timestamp,而不能定义成DATE,不然会报出字符串右截断的错误


对于DB2来说,在查询条件中可以直接用字符串指定日期或时间戳类型字段的值,例如 where create_date = '2007-04-26' 、where create_timestamp = '2007-04-26 08:08:08' ,无须使用字符串转日期函数


6、分页的处理
如果采用JDBC分页的话,注意rownum在DB2中不受支持,比如从masa_area表中取得area_id最小的10条记录,语句分别如下,注意这里的别名t书写方法

 

ORACLE: select t.* from (select rownum as r1 ,masa_area.* from masa_area order by area_id) t where t.r1<=10

DB2: select t.* from (select rownumber() over() as r1 ,masa_area.* from masa_area order by area_id) t where t.r1<=10


7、decode函数
decode函数在DB2不被支持,兼容的写法是采用case when


8、NVL函数
nvl写法在DB2不被支持,兼容的写法是采用coalesce

 

ORACLE: select NVL(f_areaid,'空') from masa_user 等同于 select coalesce(f_areaid,'空',f_areaid) from masa_user

DB2: select coalesce(f_areaid,'空',f_areaid) from masa_user


9、substr的不同
DB2 substr举例如下:

masa_group表的f_groupCode字段定义成VARCHAR(100),所以下面这个语句不会出错,如果是substr(f_groupCode,1,101)就出错了

 

select * from masa_group where substr(f_groupCode,1,50) = '001006' order by f_groupcode

 

在DB2下无错,但是

select * from masa_group where substr('001006', 1, 50) = '001006' order by f_groupcode

就报错,说第三个参数超限

这是因为'001006'已经定义为一个长度为6的charater了
这点和ORACLE有很大不同,请大家注意
如果是要从第一位取到最后一位,稳妥的办法是不要加第三个参数

 

ORACLE:select substr('123456',1) from dual

DB2:select substr('123456',1) from sysibm.sysdummy1

都没有问题

from:

http://wjxk.blog.sohu.com/82119411.html

 

比较全:

http://blog.itpub.net/14075938/viewspace-475099/

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327037171&siteId=291194637