Oracle 的函數

來源:互聯網
上載者:User

標籤:arc   入參   --   數值格式   貨幣   search   表示   中英文   space   

Oracle 的函數分類

      

 單行函數:  對每一行輸入值進行計算,得到相應的計算結果,返回給使用者,也就是說,

               每一行作為一個輸入參數,通過Function Compute得到每行的計算結果。比如說length

 多行函數:對多行輸入值進行計算,得到多行對應的多個結果,比如max,min等

單行函數

 

字元函數:

 

 用於處理字元的業務

 

  replace(char 1,search_string,replace_string)

舉例:

在emp表中的ename列中替換所有的A

 

select replace(ename,’A’,’替換後的A’) from emp;

 instr( C1,C2,I,J)

      C1  被搜尋的字串

      C2  希望搜尋的字串

      I    搜尋開始的位置

      J    出現的位置,預設為1

 

      舉例:

       select instr(‘Oracle index string function’,’s’,1,2) from dual;

 

 

 

ascii(‘要轉換的字元’)  返回與指定的字元對應的十進位數

案例:

select ascii(‘A‘) A,ascii(‘a‘) as a,ascii(‘0‘) as zero,ascii(‘ ‘) as space from dual;

 

 chr(int)  給出整數,返回對應的字元

      案例:

      select chr(54740) as zh,chr(65) as c6 from dual;

 

  concat   連接字串

     案例:

     select concat(ename,‘ 是好人‘) from emp;

     等效:

     select ename || ‘是好人’ from emp;

 

  initcap  字元轉換首字母大寫

案例:

select initcap(‘abc‘) from dual;

 

   length   計算字串長度 ,不區分中英文

   案例:

   找出emp表中字元是4的員工

   select * from emp where length(ename) = 4;

lower和upper函數   全部變小寫 / 全部變大寫

綜合:

把員工的名字首字母小寫,其他字母大寫?

select concat(lower(substr(ename,1,1)),upper(substr(ename,2,length(ename)-1))) from emp;

 

lpad 和 rpad   左填充/右填充

案例:

select lpad(‘  測試’,14,’左填左填’) from dual;

 

ltrim 和 rtrim   坐裁剪/右裁剪

案例:

select  rtrim(‘right trim oooooooo‘,‘o‘) from dual;

 

-------------------------------------------------------

裁剪全部o

 

trim   裁剪指定字元,僅支援單字元

select trim(‘t‘ from ‘the is a trim‘) from dual;

 

數字函數:

ceil函數   用於向上取整

select ceil(3.1415926) from dual;

 

floor 用於向下取整

 

  mod(m,n)  模數,如果n是0,返回m

 

round函數,用於四捨五入

 

trunk函數,用於截取一個整數

  

add_months (日期值,增加||減少的月份)

顯示最近三個月入職的員工

 

-------------------------------------------

sysdate :  顯示當前日期

last_day   顯示當月最後一天

 

next_day   顯示最近指定的日期

 

to_char(number) 轉換指定數值格式

案例:

select to_char(sal,‘L999G999D99‘) FROM emp;

 

9: 表示一個數字

L:表示本地貨幣樣式

G: 分組分隔字元(使用本地化)

D:表示逗號

 

根據deptno編號顯示不同的資訊

 

select decode(deptno,10,‘10號部門‘,20,‘20號部門‘,30,‘30號部門‘) from emp;

 

 to_date(string,format);    --將字串轉換成日期

 

舉例:

insert into emp(empno,hiredate) values(7777,to_date(‘2017-8-6‘,‘yyyy-mm-dd‘));

說明:

to_char   是使日期轉換成字元

to_date   是使字元轉換成日期

 

系統函數:

功能:用於查詢系統資訊;

視頻筆記:

 

小技巧使用子查詢完成行遷移

格式:   create table 表名 as select 列名1[ ,列名2 …]from 待覆製表 [where]

 

使用子查詢完成更新

 案例:

希望員工scott的崗位、工資、補助與smith員工一樣

 

傳統:update emp set job = (select job from emp where ename = ‘smith’),sal = (sele….),comm = (sele….) where enmae = ‘scott’;

快捷:update emp set (job,sal,comm) = (select job,sal,comm from emp where ename = ‘’smith) where ename = ‘scott’;

 

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.