oracle 經常使用函數

1、單行函數數據庫

一、字符函數函數

1.一、concat(str1,str2) 字符串拼接函數code

Select concat(‘hello’,’world’) from dual;orm

等價於索引

Select ‘hello’ || ‘world’ from dual;字符串

 

1.二、initcap(str)將每一個單詞首字母大寫,其它字母小寫string

Select initcap(‘hello world’) from dual;  è’Hello World’;it

Select initcap(‘HELLO WORLD’) from dual è’Hello World’;ast

 

1.三、instr(x,find_string[,start][,occurrence]) 返回指定字符串在某字符串中的位置,能夠指定搜索的開始位置和返回第幾回搜索出來的結果(這個位置是從1開始算起)form

Select instr(‘Hello World’,’o’) from dual;è 5

Select instr(‘Hello World’,’o’,6) from dual;è8

Select instr(‘Hello World’,’o’,1,2) from dual;è8

Select instr(‘Hello World’,’c’) from dual; è0

Instr()函數能夠充當模糊查詢,當數量十萬以上時,效果才慢慢的體現出來,數量越大,效果越明顯,並且,當查詢的字段加上索引後,instr查詢的更快

Instr(字段,’xx’)>0  è  字段  like  ‘%xx%’

Instr(字段,’xx’)=1  è  字段  like  ‘xx%’

Instr(字段,’xx’)=0  è  字段  not like  ‘%xx%’

 

1.四、length(str) 返回表達式中的字符數

Select length(‘abc’) from dual; ==》3

Select length(1234) from dual; è4

 

1.五、lower(str) 將字符串轉換爲小寫

Select lower(‘Hello World’) from dual; èhello world

 

1.六、upper(str) 將字符串轉換爲大寫

Select upper(‘hello world’) from dual; èHELLO WORLD

 

1.七、lpad(str,width[,pad_string]) 當字符串長度不夠時,左填充補齊,能夠指定補齊時用什麼字符補齊,若不指定,則以空格補齊

Selece lpad(‘hello world’,20) from dual; è         hello word

Select lpad(‘hello world’,20,’*’) from dual;è*********hello world

 

1.八、rpad(str,width[,pad_string])  同上

 

1.九、ltrim(x[,trim_string]) 從字符串左側去除指定的全部字符串,若沒有指定去除的字符串,則默認除去左側空白符

Select ltrim(‘    hello world    ‘) from dual; èhello world   

Select ltrim(‘****hello world****’) from dual;èhello world****

 

1.十、rtrim(x[,trim_string]) 從字符串右側去除指定的全部字符串,原理同ltrim()

 

1.十一、trim(trim_string from x) 從字符串中兩側去除指定的全部字符串

Select trim(‘*’  from ‘***++hello world***++’) from dual;è++hello world++

注意,ltrim()和rtrim()的截取集能夠使多個字符,但trim的截取集只能有一個字符

select trim('*+' from '***+*Hello World!***+*') from dual;  è報錯,’*+’ 是兩個字符

 

1.十二、trim(str) 去除兩邊的空格

Select trim(‘  abc  ‘) from dual; èabc

 

1.1三、nvl(x,value) 將一個null轉換爲另外一個值,若是x的值爲null,則返回value,不然返回x值自己

Select nvl(x,’值爲空’) from dual;

 

1.1四、nvl2(x,value1,value2) 若是x不爲空,返回value1,不然返回value2

 

1.1五、replace(x,search_string,replace_string) 從字符串x中搜索search_string字符串,並使用replace_string 字符串替換,並不會修改數據庫中的原始值

Select replace(‘hello world’,’hello’,’hi’) from dual; èhi world

 

1.1六、substr(x,start[,length])返回字符串中指定的字符,這些字符從字符串的第start個位置開始,長度爲length個字符;若是start是負數,則從x字符串的末尾開始算起,若是length省略,則將返回一直到字符串末尾的全部字符

Select substr(‘hello world’,3) from dual;èllo world

Select substr(‘hello world’,-3) from dual; èrld

Select substr(‘hello world’,3,2) from dual; èll

Select substr(‘hello world’,-7,4) from dual; èo wo

 

二、數值函數

2.一、abs(value) 返回value的絕對值

Select abs(-10) from dual; è10

 

2.二、ceil(value) 返回大於等於value的最小值

Select ceil(2.3) from dual; è3

 

2.三、floor(value) 返回小於等於value的最大整數

Select floor(2,3) from dual; è2

 

2.四、trunc(value,n) 對value進行截斷,若是n>0,保留n位小數;n<0,則保留-n 位整數位; n=0,則去掉小數部分

Select trunc(555.666) from dual; ==》555

Select trunc(555.666,2) from dual; è555.66;

Select trunc(555.666,-2) from dual; è500

 

三、轉換函數(將值從一種類型轉換成另外一種類型,或者從一種格式轉換成另一種格式)

3.一、to_char(x[,format]):將x轉換爲字符串,format爲轉換的格式,能夠爲數字格式或日期格式

Select to_char(‘1234.56’) from dual; è1234.56

Select to_char(‘12345.67’,’99,999.99’) from dual; è12,345.67

 

3.二、to_number(x[,format]):將x轉換爲數字,能夠指定format格式

Select to_number(‘970.13’)+25.5 from dual;è995.63

Select to_number(‘-$12,345.67’,’$99,999.99’) from dual; è -12345.67

 

3.三、cast(x as type):將x轉換爲指定的兼容的數據庫類型

select cast(12345.67 as varchar2(10)),cast('05-7月-07' as date), cast(12345.678 as number(10,2)) from dual;è 12345.67  2007/7/5   12345.68

 

3.四、to_date(x[,format]):將x字符串轉換爲日期

Select to_date(‘2018/02/15 11:10:30’,’yyyy/MM/dd hh24:mi:ss’) from dual;=> 2018/2/15 11:10:30

 

四、通用函數

4.一、decode用法

decode(字段或字段的運算,值1,值2,值3)

這個函數運行的結果是,當字段或字段的運算的值等於值1時,該函數返回值2,不然返回值3
固然值1,值2,值3也能夠是表達式。

 

4.二、sign用法

sign()函數根據某個值是0、正數仍是負數,分別返回0、一、-1

 

2、彙集函數

一、經常使用函數

1.一、avg(x):返回x的平均值

Select  avg(age)  from sc;

 

1.二、count(x)返回統計的行數

Select count(name) from sc;

 

1.三、max(x):返回x的最大值

Select max(grade) from sc;

 

1.四、min(x):返回x的最小值

Select min(grade) from sc;

 

1.五、sum(x):返回x的總計值

Select sum(grade) from sc;

 

二、對分組使用匯集函數

  對分組後的行使用匯集函數,彙集函數會統計每組中的值,對於每組分別統計後統計後按返回一個值。

2.一、分組時select子句後面的列名必須與group by子句後的列名一致,除非是聚合函數

Select  deptno,avg(sal)  from  emp;è錯誤,由於deptno不是彙集函數,也不是group by 函數

 

2.二、不能使用匯集函數做爲where子句的篩選條件

Select deptno from emp where avg(sal)>1000;è 錯誤

 

2.三、分組後,須要使用條件進行篩選,則使用having過濾分組後的行,不能使用where,where只能放在group by前面

Eg:select deptno, avg(sal)   from emp  where deptno<>10 group by deptno  having  avg(sal)>900

相關文章
相關標籤/搜索