oracle最常用簡單函數

1、字符串連接
select concat('0591','8888888') || '轉23' from dual
結果:05918888888轉23

2、首字母大寫
 
select 
initcap('zhang san') from dual
結果:Zhang San

3、INSTR(C1,C2,I,J)
 
在一個字符串中搜索指定的字符,返回發現指定的字符的位置;
C1 被搜索的字符串
C2 希望搜索的字符串
I 搜索的開始位置,默認爲1
J 出現的位置,默認爲1
select 
instr('test test','es',1,2) from dual
結果:7

4、length 長度
 
select 
length('張三') from dual       結果:2
select 
length('abc') from dual      結果:3

5.upper lower 轉成大小寫
 
select 
upper('aJkjJ') from dual  結果:AJKJJ
select 
lower('aJkjJ') from dual  結果:ajkjj

6、字符串補全
 
ipad(str,count,replaceStr)
str:指定字符串
count:總位數
replaceStr:當str的總位數小於count,那麼少出來的位置用replace補全
select 
lpad('abc',10,'*') from dual     結果:*******abc 
select 
rpad('abc',10,'*') from dual     結果:abc*******

7、字符串去除空格
select ltrim('    abc           ') from dual   去除左空格
select rtrim('    abc           ') from dual   去除右空格

trim()沒有參數:表示去除所有的空格
select trim('    abc           ') from dual    去除左右空格

trim(s from str)表示把str中的s部分全部刪除掉
select trim(0 from 7500) from dual        結果:75
select trim('0' from '     7500       ') from dual         結果:     7500      所以要想刪除,必須要先把空格先去除
select trim('0' from trim('     7500       ')) from dual   結果:75


8、字符串替換
select replace('abc','b','d') from dual 結果:adc

9、取餘數
select mod(10,3) from dual    結果:1
select mod(3,3) from dual     結果:0
select mod(2,3) from dual     結果:2

10、四捨五入
ROUND和TRUNC
按照指定的精度進行舍入
SQL> select round(55.5),round(-55.4),trunc(55.5),trunc(-55.5) from dual;
結果分別是:56 -55 55 -55

11、小數的精度
select 
trunc(123.456,1) from dual 結果:123.4

12、截取字串符
select substr('abcdefg',2,3) from dual
結果:bcd

13、case 的用法
select (case when 'x'='x' then else end) from dual                               結果:1
select (case when 60>50 then 1 when 50>60 then 2 end) from dual      結果:1

14、判斷是否爲空
NVL(expr1, expr2)->expr1爲NULL,返回expr2;不爲NULL,返回expr1。注意兩者的類型要一致 
select nvl(null,'abc') from dual   結果:abc

NVL2 (expr1, expr2, expr3) ->expr1不爲NULL,返回expr2;爲NULL,返回expr3。expr2和expr3類型不同的話,expr3會轉換爲expr2的類型 
select nvl2(null,null,'abc') from dual    結果:abc

15、是否相等
NULLIF (expr1, expr2) ->相等返回空串,不等返回expr1
select 
nullIf('abcd','abc') from dual     結果:abcd

 

16.轉換成NUM

to_number(code))

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章