oracle常用函數及樣本

來源:互聯網
上載者:User

標籤:資料庫   where   聚集合函式   class   number   筆記   substr   字元   篩選   

 學習oracle也有一段時間了,發現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的位置,結果為5select instr(‘Hello World!‘,‘o‘,6) from dual;--從6位置開始搜尋,返回第一次出現的o的位置,結果為8select instr(‘Hello World!‘,‘o‘,1,2) from dual;--從1位置開始搜尋,返回第二次出現o的位置,結果為8

  (4)length(str)返回運算式中的字元數

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

  (5)lengthb(str)返回運算式中的位元組數

select lengthb(‘Hello World!‘) from dual;--返回結果為12select 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.66select 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.67select 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.67select 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;

oracle常用函數及樣本

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.