關於處理小數點位數的幾個oracle函數()
1. 取四舍五入的幾位小數
select round(1.2345, 3) from dual;
結果:1.235
2. 保留兩位小數,只舍
select trunc(1.2345, 2) from dual;
結果:1.23
select trunc(1.2399, 2) from dual;
結果:1.23
3.取整數
返回大於或等於x的最大整數:
SQL> select ceil(23.33) from dual;
結果: 24
返回等於或小於x的最大整數:
SQL> select floor(23.33) from dual;
結果: 23
返回舍入到小數點右邊y位的x值:rcund(x,[y])
SQL> select round(23.33) from dual;
結果: 23
返回截尾到y位小數的x值:trunc(x,[y])
SQL> select trunc(23.33) from dual;
結果: 23
格式化數字
The following are number examples for the to_char function.
to_char(1210.73, '9999.9') would return '1210.7'
to_char(1210.73, '9,999.99') would return '1,210.73'
to_char(1210.73, '$9,999.00') would return '$1,210.73'
to_char(21, '000099') would return '000021'
to_char函數特殊用法
to_char(sysdate,'d') 每周第幾天
to_char(sysdate,'dd') 每月第幾天
to_char(sysdate,'ddd') 每年第幾天
to_char(sysdate,'ww') 每年第幾周
to_char(sysdate,'mm') 每年第幾月
to_char(sysdate,'q') 每年第幾季
to_char(sysdate,'yyyy') 年
比如要找某個時間為每周第幾天就可以
SQL> select to_char(to_date('20070101','yyyymmdd'),'d') from dual;
1.instr
在Oracle/PLSQL中,instr函數返回要截取的字符串在源字符串中的位置。
語法如下:instr( string1, string2 [, start_position [, nth_appearance ] ] )
string1 源字符串,要在此字符串中查找。
string2 要在string1中查找的字符串.
start_position 代表string1 的哪個位置開始查找。此參數可選,如果省略默認為1. 字符串索引從1開始。如果此參數為正,從左到右開始檢索,如果此參數為負,從右到左檢索,返回要查找的字符串在源字符串中的開始索引。
nth_appearance 代表要查找第幾次出現的string2. 此參數可選,如果省略,默認為 1.如果為負數系統會報錯。
注意:
如果String2在String1中沒有找到,instr函數返回0.
應用於:
Oracle 8i, Oracle 9i, Oracle 10g, Oracle 11g
舉例說明:
select instr('abc','a') from dual; -- 返回 1
select instr('abc','bc') from dual; -- 返回 2
select instr('abc abc','a',1,2) from dual; -- 返回 5
select instr('abc','bc',-1,1) from dual; -- 返回 2
select instr('abc','d') from dual; -- 返回 0
注:也可利用此函數來檢查String1中是否包含String2,如果返回0表示不包含,否則表示包含。