在DB2中如何實現Oracle的相關功能(二)
作者﹕CCBZZP
在現實的應用中大家可能經常會碰到在DB2中如何實現Oracle的某些功能﹐
在此我簡單地總結一下﹐實現某一功能可能會有很多種方法﹐在此就沒有全部列出﹐
歡迎大家繼續﹐以便和大家共享﹐共同探討﹐共同近步﹗(以下主要以Oracle
8I,9I和DB2 7.X為例)。
1.如何查看數据庫的版本的Oracle和DB2的寫法
Oracle 可以這樣實現﹕
SQL> connect system/manager124@test;
已連線.
SQL> select * from v$version;
BANNER
----------------------------------------------------------------
Oracle9i Enterprise Edition Release 9.2.0.1.0 - ProdUCtion
PL/SQL Release 9.2.0.1.0 - Production
CORE 9.2.0.1.0 Production
TNS for 32-bit Windows: Version 9.2.0.1.0 - Production
NLSRTL Version 9.2.0.1.0 - Production
DB2 可以這樣實現﹕
在命令窗口執行 db2level
D: QLLIB\BIN>db2level
DB210851 Instance "DB2" uses DB2 code release "SQL07020" with level identifier
"03010105" and informational tokens "DB2 v7.1.0.40","n010415" and "WR21254".
2.如何快速清空一個大表的Oracle和DB2的寫法
Oracle 可以這樣實現﹕
SQL>truncate table table_name;
DB2 可以這樣實現﹕
alter table table_name active not logged initially with empty table;
3.如何查看表空間的使用狀況的Oracle和DB2的寫法
Oracle 可以這樣實現﹕
SELECT tablespace_name, max_m, count_blocks free_blk_cnt, sum_free_m,to_char(100*sum_free_m/sum_m, '99.99') '%' AS
pct_free
FROM ( SELECT tablespace_name,sum(bytes)/1024/1024 AS sum_m FROM dba_data_files GROUP BY tablespace_name),
( SELECT tablespace_name AS fs_ts_name, max(bytes)/1024/1024 AS max_m, count(blocks) AS count_blocks,
sum(bytes/1024/1024) AS sum_free_m FROM dba_free_space GROUP BY tablespace_name )
WHERE tablespace_name = fs_ts_name
DB2 可以這樣實現﹕
list tablespace containers for 你的表空間號 show detail;
4.如何從一時間點取出日期的各部分的常用的Oracle和DB2的寫法
Oracle 可以這樣實現﹕
1>.取時間點的年份的寫法:
SELECT TO_CHAR(SYSDATE,'YYYY') FROM DUAL;
2>.取時間點的月份的寫法:
SELECT TO_CHAR(SYSDATE,'MM') FROM DUAL;
3>.取時間點的日的寫法:
SELECT TO_CHAR(SYSDATE,'DD') FROM DUAL;
4>.取時間點的時的寫法:
SELECT TO_CHAR(SYSDATE,'HH24') FROM DUAL;
5>.取時間點的分的寫法:
SELECT TO_CHAR(SYSDATE,'
MI') FROM DUAL;
6>.取時間點的秒的寫法:
SELECT TO_CHAR(SYSDATE,'SS') FROM DUAL;
7>.取時間點的毫秒的寫法:(9I以上)
select SUBSTR(systimestamp,20,6) from dual;
8>.取時間點的日期的寫法:
SELECT TRUNC(SYSDATE) FROM DUAL;
9>.取時間點的時間的寫法:
SELECT TO_CHAR(SYSDATE,'HH24:MI:SS') FROM DUAL;
10>.日期,時間形態變為字符形態
SELECT TO_CHAR(SYSDATE) FROM DUAL;
11>.將字符串轉換成日期或時間形態:
SELECT TO_DATE('2003/08/01') FROM DUAL;
12>.返回參數的星期几的寫法:
SELECT TO_CHAR(SYSDATE,'D') FROM DUAL;
13>.返回參數一年中的第几天的寫法:
SELECT TO_CHAR(SYSDATE,'DDD') FROM DUAL;
14>.返回午夜和參數中指定的時間值之間的秒數的寫法:
SELECT TO_CHAR(SYSDATE,'SSSSS') FROM DUAL;
15>.返回參數中一年的第几周的寫法:
SELECT TO_CHAR(SYSDATE,'WW') FROM DUAL;
DB2 可以這樣實現﹕
1>.取時間點的年份的寫法:
SELECT YEAR(current timestamp) FROM SYSIBM.SYSDUMMY1;
2>.取時間點的月份的寫法:
SELECT MONTH(current timestamp) FROM SYSIBM.SYSDUMMY1;
3>.取時間點的日的寫法:
SELECT DAY(current timestamp) FROM SYSIBM.SYSDUMMY1;
4>.取時間點的時的寫法:
SELECT HOUR(current timestamp) FROM SYSIBM.SYSDUMMY1;
5>.取時間點的分的寫法:
SELECT MINUTE(current timestamp) FROM SYSIBM.SYSDUMMY1;
6>.取時間點的秒的寫法:
SELECT SECOND(current timestamp) FROM SYSIBM.SYSDUMMY1;
7>.取時間點的毫秒的寫法:
SELECT MICROSECOND(current timestamp) FROM SYSIBM.SYSDUMMY1;
8>.取時間點的日期的寫法:
SELECT DATE(current timestamp) FROM SYSIBM.SYSDUMMY1;
9>.取時間點的時間的寫法:
SELECT TIME(current timestamp) FROM SYSIBM.SYSDUMMY1;
10>.日期,時間形態變為字符形態:
SELECT char(current date) FROM SYSIBM.SYSDUMMY1;
SELECT char(current time) FROM SYSIBM.SYSDUMMY1;
SELECT char(current date+12 hours) FROM SYSIBM.SYSDUMMY1;
11>.將字符串轉換成日期或時間形態:
SELECT TIMESTAMP('2002-10-20-12.00.00.000000') FROM SYSIBM.SYSDUMMY1;
SELECT TIMESTAMP('2002-10-20 12:00:00') FROM SYSIBM.SYSDUMMY1;
SELECT DATE('2002-10-20') FROM SYSIBM.SYSDUMMY1;
SELECT DATE('10/20/2002') FROM SYSIBM.SYSDUMMY1;
SELECT TIME('12:00:00'
) FROM SYSIBM.SYSDUMMY1;
SELECT TIME ('12.00.00') FROM SYSIBM.SYSDUMMY1;
12>.返回參數的星期几的寫法:
SELECT DAYNAME(current timestamp) FROM SYSIBM.SYSDUMMY1;
SELECT DAYOFWEEK(current timestamp) FROM SYSIBM.SYSDUMMY1;
SELECT DAYOFWEEK_ISO(current timestamp) FROM SYSIBM.SYSDUMMY1;
13>.返回參數一年中的第几天的寫法:
SELECT DAYOFYEAR(current timestamp) FROM SYSIBM.SYSDUMMY1;
14>.返回午夜和參數中指定的時間值之間的秒數的寫法:
SELECT MIDNIGHT_SECONDS(current timestamp) FORM SYSIBM.SYSDUMMY1;
15>.返回參數中一年的第几周的寫法:
SELECT WEEK(current timestamp) FORM SYSIBM.SYSDUMMY1;
待續...