1.Nvl()函數
2.Decode()函數
select decode( x , 1 ,‘xis 1’, 2 ,‘xis 2’,‘others’)from dual
當x等於1時,則返回‘xis 1’。
當x等於2時,則返回‘xis 2’。
否則,返回others’。
3.Rpad()函數
rpad函數從右邊對字符串使用指定的字符進行填充
rpad(string,padded_length,[pad_string])
string 表示:被填充的字符串
padded_length 表示:字符的長度,是返回的字符串的數量,如果這個數量比原字符串的長度要短,rpad函數將會把字符串截取成從左到右的n個字符;
pad_string 是個可選參數,這個字符串是要粘貼到string的右邊,如果這個參數未寫,lpad函數將會在string的右邊粘貼空格。
例如:
rpad('tech', 7); 將返回'tech '
rpad('tech', 2); 將返回'te'
rpad('tech', 8, '0'); 將返回'tech0000'
rpad('tech on the net', 15, 'z'); 將返回 'tech on the net'
rpad('tech on the net', 16, 'z'); 將返回 'tech on the netz'
4.Substr()函數
oracle的substr函數的用法
取得字符串中指定起始位置和長度的字符串 substr( string, start_position, [ length ] )
substr('This is a test', 6, 2) would return 'is'
substr('This is a test', 6) would return 'is a test'
substr('TechOnTheNet', -3, 3) would return 'Net'
substr('TechOnTheNet', -6, 3) would return 'The'
select substr('Thisisatest', -4, 2) value from dual 結果是 te
select substr('emros',-3,1) value from dual 結果是 r
5.Round()函數
語法爲ROUND(number,num_digits)其中Number是需要進行四捨五入的數字;Num_digits爲指定的位數,按此位數進行四捨五入,如果 num_digits 大於 0,則四捨五入到指定的小數位,如果 num_digits 等於 0,則四捨五入到最接近的整數,如果 num_digits 小於 0,則在小數點左側進行四捨五入。
例如:
ROUND(21.129,0) 將 21.129 四捨五入到一個整數結果爲21。
ROUND(21.129,2) 將 21.129 四捨五入到兩個小數位,結果爲21.13。
ROUND(21.129,1) 將 21.129 四捨五入到一個小數位結果爲21.1。
ROUND(-21.129,2) 將 -21.129 四捨五入到兩小數位結果爲-21.13。
ROUND(21.129, -1) 將 21.129 四捨五入到小數點左側一位結果爲20。
6.add_months()函數
7.RTrim()函數
Oracle數據庫百分比格式化
SELECT RTrim(To_Char((1/3)*100,'FM99999999990.9999'),'.')||'%' FROM dual; 這樣獲得的是33.3333%
SELECT substr(RTrim(To_Char((1/3)*100,'FM99999999990.9999'),'.'),1,2) ||'%' FROM dual; 這樣可以省略小數,得33%
SELECT RTrim(To_Char((round((1/3),2))*100,'FM99999999990.9999'),'.')||'%' FROM dual;與以上是同樣效果,得33%.
第一種解決方法:
SELECT DECODE (TRUNC (1/100),0, REPLACE (1/100, '.', '0.'),TO_CHAR (1/100))
FROM DUAL;
大家只要在1/100的地方替換成自己需要的百分數就可以了。
第二種解決方法:
SELECT RTrim(To_Char(1/100,'FM99999999990.9999'),'.') FROM dual;
替換方式和上面相同。
兩種方式的詳細比較:
第一種公式比較複雜,但可以處理通用的數值。
第二種看起來簡單,不過格式化的值的位數一定要大於計算的位數才行。比如 SELECT RTrim(To_Char(200,'FM90.9999') ,'.') FROM dual; 顯示的就是########,因爲已經超過了格式化的範圍。