1. 程式人生 > >oracle的各種常用函式大全

oracle的各種常用函式大全

1.字元函式

  (1)concat(str1,str2)字串拼接函式

select concat('Hello ','World') from dual;
--等價於
select 'Hello '||'World' from dual;

  (2)initcap(str)將每個單詞首字母大寫,其他字母小寫

select initcap('hello world!') from dual; --返回結果為'Hello World!'
select initcap('HELLO WORLD!') from dual; --返回結果為'Hello World!'

  (3)instr(x,find_string[,start][,occurrence])返回指定字串在某字串中的位置,可以指定搜尋的開始位置和返回第幾次搜尋出來的結果

----------搜尋時下標從1開始計算
select instr('Hello World!','o') from dual;--從1位置開始搜尋,返回第一次出現的o的位置,結果為5
select instr('Hello World!','o',6) from dual;--從6位置開始搜尋,返回第一次出現的o的位置,結果為8
select instr('Hello World!','o',1,2) from dual;--從1位置開始搜尋,返回第二次出現o的位置,結果為8

  (4)length(str)返回表示式中的字元數

select length('Hello World!') from dual;--返回結果為12
select length('張三') from dual;--返回結果為2

  (5)lengthb(str)返回表示式中的位元組數

select lengthb('Hello World!') from dual;--返回結果為12
select lengthb('張三') from dual;--返回結果為6

  (6)lower(str)將字串轉換為小寫

select lower('Hello World!') from dual;

  (7)upper(str)將字串轉換為大寫

select upper('Hello World!') from dual;

  (8)lpad(str,width[,pad_string])當字串長度不夠時,左填充補齊,可以指定補齊時用什麼字元補齊,若不指定,則以空格補齊

select lpad('Hello World!',20) from dual;--返回結果為'        Hello World!'
select lpad('Hello World!',20,'*') from dual;--返回結果為'********Hello World!'

  (9)rpad(str,width[,pad_string])當字串長度不夠時,右填充補齊,原理同左填充

select rpad('Hello World!',20) from dual;--返回結果為'Hello World!        '
select rpad('Hello World!',20,'*+') from dual;--返回結果為'Hello World!*+*+*+*+'

  (10)ltrim(x[,trim_string])從字串左側去除指定的所有字串,若沒有指定去除的字串,則預設去除左側空白符

select ltrim('   Hello World!    ') from dual;--返回結果為'Hello World!    '
select ltrim('***+*Hello World!***+*','*+') from dual;--返回結果為'Hello World!***+*'

  (11)rtrim(x[,trim_string])從字串右側去除指定的所有字串,原理同ltrim()

select rtrim('   Hello World!    ') from dual;--返回結果為'    Hello World!'
select rtrim('***+*Hello World!***+*','*+') from dual;--返回結果為'***+*Hello World!'

  (12)trim(trim_string from x)從字串兩側去除指定的所有字串

select trim('*+' from '***+*Hello World!***+*') from dual;

     注意,ltrim()和rtrim()的擷取集可以使多個字元,但trim的擷取集只能有一個字元

select trim('*+' from '***+*Hello World!***+*') from dual;--錯誤,擷取集只能有一個字元

  (13)nvl(x,value)將一個NULL轉換為另外一個值,如果x為NULL,則返回value,否則返回x值本身

insert into student values(7,'豬豬',default,NULL);
select nvl(address,'北京市') from student;

  (14)nvl2(x,value1,value2),如果x不為NULL,返回value1,否則,返回value2

select nvl2(address,'有地址','無地址') from student;

  (15)replace(x,search_string,replace_string),從字串x中搜索search_string字串,並使用replace_string字串替換。並不會修改資料庫中原始值

select replace('Hello World!','o','HA') from dual;

  (16)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.數值函式

  (1)abs(value)返回value的絕對值

select abs(-10) from dual;--返回結果為10

  (2)ceil(value)返回大於等於value的最小整數

select ceil(2.3) from dual; --返回結果為3

  (3)floor(value)返回小於等於value的最大整數

select floor(2.3) from dual; --返回結果為2

  (4)trunc(value,n)對value進行截斷,如果n>0,保留n位小數;n<0,則保留-n位整數位;n=0,則去掉小數部分

select trunc(555.666) from dual; --返回結果為555,不加n時預設去掉小數部分
select trunc(555.666,2) from dual;--返回結果為555.66
select trunc(555.666,-2) from dual;--返回結果為500

  (5)round(value,n)對value進行四捨五入,儲存小數點右側的n位。如果n省略的話,相當於n=0的情況

select round(555.666) from dual;--返回結果為556,不加n時預設去掉小數部分
select round(555.666,2) from dual;--返回結果為555.67
select round(555.666,-2) from dual;--返回結果為600

         注意:1.trunc和round用法類似,只不過trunc是硬生生擷取,並不進行四捨五入,而round進行擷取時四捨五入          2.都還可以對日期的擷取,可以參考寫的日期函式筆記

select round(sysdate,'year') from dual;
select trunc(sysdate,'year') from dual;

3.轉換函式

  將值從一種型別轉換成另外一種型別,或者從一種格式轉換為另外一種格式

  (1)to_char(x[,format]):將x轉化為字串。 format為轉換的格式,可以為數字格式或日期格式

select to_char('12345.67') from dual; --返回結果為12345.67
select to_char('12345.67','99,999.99') from dual; --返回結果為12,345.67

  (2)to_number(x [,  format]):將x轉換為數字。可以指定format格式

select to_number('970.13') + 25.5 from dual;
select to_number('-$12,345.67', '$99,999.99') from dual;

  (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;

  (4)to_date(x [,format]):將x字串轉換為日期

select to_date('2012-3-15','YYYY-MM-DD') from dual

二.聚集函式

1.常用函式

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

select avg(grade) from sc;

  (2)count(x):返回統計的行數

select count(name) from sc;

  (3)max(x):返回x的最大值

select max(grade) from sc;

  (4)min(x):返回x的最小值

select min(grade) from sc;

  (5)sum(x):返回x的總計值

select sum(grade) from sc;

2.對分組行使用聚集函式

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

  示例

--按照職位分組,求出每個職位的最高和最低工資
select job ,max(sal),min(sal) from emp 
group by job 
order by job;

注意:1.分組時select子句後邊的列名必須與group by子句後的列名一致,除非是聚合函式

select deptno,avg(sal) from EMP;--錯誤,因為deptno不是聚集函式,也不是group by後面跟的列名

2.不能使用聚集函式作為WHERE子句的篩選條件

select deptno from emp where avg(sal)>1000;--錯誤

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

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