【原創】cast() 函式的用處

木頭一個發表於2008-06-01

大家知道從number、char、date型別之間有to_number、to_char、to_date函式進行型別轉換。但是如何進行number、binary_float、binary_double型別之間的轉換?或number型別如何轉換精度、有效值?這就需要用到cast函式

cast的函式主要有下面兩個作用:

1、純格式轉換

number、binary_float、binary_double型別之間的轉換:
sys@T>select a from t1;

                     A
----------------------
  123567890.1234567890

sys@T>select cast(a as binary_float) from t1;

CAST(AASBINARY_FLOAT)
---------------------
           1.236E+008

sys@T>select cast(a as binary_double) from t1;

CAST(AASBINARY_DOUBLE)
----------------------
            1.236E+008

sys@T>select dump(a) from t1;

DUMP(A)
-------------------------------------------------------
Typ=2 Len=11: 197,2,24,57,79,91,13,35,57,79,91

sys@T>select dump(cast(a as binary_float)) from t1;

DUMP(CAST(AASBINARY_FLOAT))
-------------------------------------------------------
Typ=100 Len=4: 204,235,175,226

sys@T>select dump(cast(a as binary_double)) from t1;

DUMP(CAST(AASBINARY_DOUBLE))
-------------------------------------------------------
Typ=101 Len=8: 193,157,117,252,72,126,107,117

number型別精度、有效值的轉換:
sys@T>select  a from t1;

                     A
----------------------
  123567890.1234567890

sys@T>select cast(a as number(10)) from t1;

CAST(AASNUMBER(10))
-------------------
          123567890

sys@T>select dump(a) from t1;

DUMP(A)
-------------------------------------------------------
Typ=2 Len=11: 197,2,24,57,79,91,13,35,57,79,91

sys@T>select dump(cast(a as number(10))) from t1;

DUMP(CAST(AASNUMBER(10)))
-------------------------------------------------------
Typ=2 Len=6: 197,2,24,57,79,91

2、提高效能
Oracle對於number的運算是在軟體中模擬的,而對於binary_float、binary_double這種浮點數是使用硬體執行計算的。對於複雜的
數學函式,使用number和使用浮點數的效能現比就會有比較大的差距。所以可以在對number計算時,將其轉換為浮點數進行運算。

sys@T>insert into t1 select level from dual connect by level<=100000;

在tkprof中可以看到number型別使用的CPU時間是binary_float的10倍:
select ln(a) from t1

call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        1      0.00       0.00          0          1          0           0
Execute      1      0.00       0.00          0          0          0           0
Fetch     6668      3.85       3.89          0       6823          0      100000
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total     6670      3.85       3.89          0       6824          0      100000


select ln(cast(a as binary_float)) from t1

call     count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse        1      0.00       0.01          0          1          0           0
Execute      1      0.00       0.00          0          0          0           0
Fetch     6668      0.40       0.37          0       6823          0      100000
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total     6670      0.40       0.39          0       6824          0      100000

但是,使用這種方法一定要小心精度的損失

sys@T>insert into t1 values(123567890.1234567890);

1 row created.

sys@T>select ln(a) from t1;

     LN(A)
----------
18.6323013

sys@T>select ln(cast(a as binary_float)) from t1;

LN(CAST(AASBINARY_FLOAT))
-------------------------
               1.863E+001

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/498744/viewspace-328471/,如需轉載,請註明出處,否則將追究法律責任。

相關文章