1. 程式人生 > >轉換函式:TO_CHAR()用法之一 將一個數值轉化成字串

轉換函式:TO_CHAR()用法之一 將一個數值轉化成字串

 格式:TO_CHAR(number,format) 即 TO_CHAR((數值,格式)

用法: 1、不指定格式的 TO_CHAR函式將數值轉換成簡單字串形式。

        例:  TO_CHAR(123)       結果       123

              TO_CHAR(-123)      結果      -123

        2、用元素9的格式

         例:  TO_CHAR(1234,'9999')       結果        1234

               TO_CHAR(567,'9999')        結果         567

               TO_CHAR(-567,'9999')       結果        -567

               TO_CHAR(1234567,'9999')    結果        ####

               TO_CHAR(45.789,'9999')     結果          46

               TO_CHAR(567,'9999.99')       結果       567.00

               TO_CHAR(-567,'9999.99')      結果      -567.00

               TO_CHAR(1234567,'9999.99')   結果      #######

               TO_CHAR(45.789,'9999.99')    結果        45.79

               TO_CHAR(567,'9,999,999')        結果             567

               TO_CHAR(-567,'9,999,999')       結果            -567

               TO_CHAR(1234567,'9,9999,999')   結果        1,234,567

               TO_CHAR(0.44,'9,999,999')       結果                0

        3、用元素$的格式

         例:  TO_CHAR(1234,'$9999')       結果        $1234

               TO_CHAR(-567,'$9999')       結果        -$567

        4、用元素B的格式  如果被轉換數值整數部分是0就顯示為空格

         例:  TO_CHAR(567,'B999.9')         結果             567.0

               TO_CHAR(-567,'B999.9')        結果            -567.0

               TO_CHAR(0.44,'B999.9')        結果                .4

        5、用元素MI的格式  負數則轉換成末尾帶減號的數

         例:  TO_CHAR(567,'999MI')         結果             567

               TO_CHAR(-567,'999MI')        結果             567-

         6、用元素S的格式  轉換成帶有正負號的數字字元

         例:  TO_CHAR(567,'S999')         結果            +567

               TO_CHAR(-567,'S999')        結果            -567

               TO_CHAR(567,'999S')         結果             567+

               TO_CHAR(-567,'999S')        結果             567-

         7、用元素PR的格式  用尖括號包圍負數

         例:  TO_CHAR(567,'9999PR')         結果           567

               TO_CHAR(-567,'9999PR')        結果          <567>

         8、用元素D的格式  在指定位置插入小數點

          例:  TO_CHAR(567,'9999D99')          結果           567.00

                TO_CHAR(-567,'9999D99')         結果          -567.00

                TO_CHAR(567.866,'9999D99')      結果           567.87

         9、EEEE 科學計數法

         例:  TO_CHAR(567,'9.9EEEE')          結果             5.7E+02

               TO_CHAR(45,'9.9EEEE')           結果             4.5E+01

               TO_CHAR(0.0666,'9.9EEEE')       結果             6.7E-02    

TO_CHAR函式名不虛傳, 轉換功能很強

原文轉自於: http://blog.sina.com.cn/s/blog_4cbadf5a0100f7u7.html