Oracle和db2 sql语句的区别

1、取前N条记录

Oracle:Select*fromTableNamewhererownum<=N;

DB2:Select*fromTableNamefetchfirstNrowsonly;

2、取得系统日期

Oracle:Selectsysdatefromdual;

DB2:Selectcurrenttimestampfromsysibm.sysdummy1;

3、空值转换

Oracle:Selectproductid,loginname,nvl(cur_rate,'0')fromTableName;

DB2:Selectproductid,loginname,value(cur_rate,'0')fromTableName;

Coalesce(cur_rate,'0')

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

Oracle:selectto_char(sysdate,'YYYY-MM-DDHH24:MI:SS')fromdual;

DB2:selectvarchar(currenttimestamp)fromsysibm.sysdummy1;

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

lDB2数据类型改变函数:char()、varchar()、int()、date()、time()等;取得年,月,日等的写法:YEAR(currenttimestamp),MONTH(currenttimestamp),DAY(currenttimestamp),HOUR(currenttimestamp),MINUTE(currenttimestamp),SECOND(currenttimestamp),MICROSECOND(currenttimestamp),只取年月日可以用DATE(currenttimestamp),取时分秒TIME(currenttimestamp)。Char()是定长字符串(1-255),varchar()为非定长字符串(1-32672)日期,时间形态变为字符形态:char(currentdate),char(currenttime)将字符串转换成日期或时间形态:TIMESTAMP('2002-10-2012:00:00'),DATE('2002-10-20'),DATE('10/20/2002'),TIME('12:00:00')

l目前DB2V8也支持to_char和to_date

5、快速清空大表

Oracle:truncatetableTableName;

DB2:altertableTableNameactivenotloggedinitiallywithemptytable;

6、关于ROWID

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

7、To_Number

Oracle:selectto_number('123')fromdual;

DB2:selectcast('123'asinteger)fromsysibm.sysdummy1;

SELECTCAST(currenttimeaschar(8))FROMsysibm.sysdummy1

8、创建类似表

Oracle:createtableaasselect*fromb;

DB2:createtablealikeb;

CREATETABLEtab_newASselectcol1,col2…FROMtab_oldDEFINITIONONLY(8版有效,9版无效)

9、decode方法

Oracle:decode方法(DECODE(条件,值1,翻译值1,值2,翻译值2,...值n,翻译值n,缺省值))或者case语句DB2中只有CASE表达式SELECTid,name,CASEWHENinteger(flag)=0THEN‘假’WHENinteger(flag)=1THEN‘真’ELSE‘异常’ENDFROMTEST或者SELECTid,name,CASEinteger(flag)WHEN0THEN‘假’WHEN1THEN‘真’ELSE‘异常’ENDFROMTEST

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

Oracle:直接用子查询

Db2:with语句WITHa1AS(selectmax(id)asaa1fromtest)selectid,aa1fromtest,a1

11、数据类型

比较大的差别:

Oracle:char2000

DB2:char254

Oracle:datedatetime

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

相关推荐