Oracle和DB2的SQL語句區別

2021-08-31 05:06:10 字數 2530 閱讀 3391

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:日期時間

db2和oracle中的樹查詢(遞迴查詢)語句

在db2和oracle中的對樹的遞迴查詢語句。表結構 create table main node mla id integer not null 節點id mla rootid integer,根節點id mla parentid integer,父節點id mla name varchar2 5...

Oracle和DB2的部分SQLCODE對應表

最近在做oracle到db2的轉換,整理了部分sqlcode的對應關係,部分已經新增了中文描述 因為轉換的程式為pro c程式,增加了sqlcode的巨集定義 sqlcode巨集定義 oracle db2中 描述 m no data found 1403 100未能找到資料 m dup val on...

DB2和ORACLE的印象比較

乙個專案需要 db2,以前沒用過,用了windows 2008下的db29.7 好幾周,算入門了吧,下面這文章就當乙個 oracle使用者眼裡的db2的印象吧,也許有些東西可能有偏差,但是就是我自己的印象 2 管理工具,db2的管理工具做得太簡陋了,控制中心只能說能用而已,而且還有好多bug,不如o...