Oracle常用函數匯總

來源:互聯網
上載者:User

標籤:style   http   color   io   os   ar   strong   for   資料   

在Oracle OCP考試中,相當一部分知識點涉及到對於Oracle常見函數的考查。儘管Oracle官方文檔SQL Language Reference中Functions一章內列舉了所有Oracle內建函數,但如果要系統的看一遍,還是要花費相當的精力,更何況還是英文呢。如果碰到一個不熟悉的,就查一下,不經常用,又很容易遺忘。下面就對Oracle常見函數做個整理,方便以後查詢。

一、 大小寫轉換函式

1. LOWER函數

LOWER函數將字元以小寫形式輸出。其文法為LOWER(char)。

例如:

SQL> select lower(100+100),lower(sysdate),lower(‘HELLO‘) from dual;

LOW LOWER(SYS LOWER
---    ---------      -----
200  25-sep-14    hello

注意:返回的均是字元類型,在執行LOWER函數之前先計算資料和日期運算式,並隱式轉換為字元資料。

2. UPPER函數

UPPER函數將字元以大寫形式輸出。其文法為UPPER(char)。

例如:

SQL> select upper(‘hello world‘) from dual;

UPPER(‘HELL
-----------
HELLO WORLD

3. INITCAP函數

INITCAP函數將字串中每個單詞的第一個字母大寫,而該單詞的剩餘字母小寫。

例如:

SQL> select initcap(‘hello WORLD! that is funny!‘) from dual;

INITCAP(‘HELLOWORLD!THATISF
---------------------------
Hello World! That Is Funny!

二、 字元操作函數

1. CONCAT函數

CONCAT函數用於串連兩個字串。有兩個參數,文法為CONCAT(s1,s2)。

例如:

SQL> select concat(‘hello‘,‘world‘) from dual;

CONCAT(‘HE
----------
helloworld

SQL> select concat(concat(ename,‘‘‘s job is ‘),job) from emp where empno=7788;

CONCAT(CONCAT(ENAME,‘‘‘SJOBIS
-----------------------------
SCOTT‘s job is ANALYST

2. LENGTH函數

LENGTH函數返回字串的字元數,包括空格,定位字元和特殊字元。其文法為LENGTH(s)。

例如:

SQL> select length(‘hello world!‘) from dual;

LENGTH(‘HELLOWORLD!‘)
---------------------
12

3. LPAD和RPAD函數

LPAD和RPAD函數分別被稱為左填充和右填充函數。它們分別在給定字串左邊或右邊填充指定數量的字元。其文法為LPAD(s,n,p)和RPAD(s,n,p),其中s表示源字串,n表示返回字串的最終長度,p指定用於填充的字串。注意:如果參數n小於或者等於源字串s的長度,就不會添加任何字元,此時只返回源字串s的前n個字元。p預設為空白格。

例如:

SQL> select lpad(‘hello‘,10,‘*‘),lpad(‘hello‘,10,‘*#‘),lpad(‘hello‘,10),lpad(‘hello‘,4,‘*‘) from dual;

LPAD(‘HELL LPAD(‘HELL LPAD(‘HELL LPAD
----------     ----------      ----------    ----
*****hello  *#*#*hello        hello    hell

SQL> select rpad(‘hello‘,10,‘*‘),rpad(‘hello‘,10,‘*#‘),rpad(‘hello‘,10),rpad(‘hello‘,4,‘*‘) from dual;

RPAD(‘HELL RPAD(‘HELL   RPAD(‘HELL RPAD
----------     ----------       ----------     ----
hello*****  hello*#*#*   hello            hell

4. TRIM函數

TRIM函數從字串的開頭或者結尾刪除一些字元。其文法為TRIM([trailing|leading|both] trimstring from s)。其中

TRIM(s)刪除輸入字串兩邊的空格

TRIM(trailing trimstring from s)從字串s的結尾刪除所有trimstring(如果存在的話)。

TRIM(leading trimstring from s)從字串s的開頭刪除所有trimstring。

TRIM(both trimstring from s)從字串s的開頭和結尾刪除所有trimstring。

SQL> select trim(both ‘*‘ from ‘***hello****‘) from dual;

TRIM(
-----
hello

SQL> select trim(trailing ‘*‘ from ‘***hello****‘) from dual;

TRIM(TRA
--------
***hello

SQL> select trim(leading ‘*‘ from ‘***hello****‘) from dual;

TRIM(LEAD
---------
hello****

注意:trimstring只能是一個字元。

SQL> select trim(leading ‘*!‘ from ‘*!*!hello‘) from dual;
select trim(leading ‘*!‘ from ‘*!*!hello‘) from dual
*
ERROR at line 1:
ORA-30001: trim set should have only one character

5. INSTR函數(In-string)

INSTR函數確定搜尋字串在給定字串中的位置。它返回數字位置,如果搜尋字串不存在,則該函數返回0。其文法為INSTR(source string,search string,[search start position],[nth occurrence])。其中,後兩個參數是可選的,search start position的預設值是1,即source string的開頭。nth occurrence的預設值是1,即第一次出現。

例如:

SQL> select instr(‘1*3*5*7*9‘,‘*‘,-3,2) from dual;

INSTR(‘1*3*5*7*9‘,‘*‘,-3,2)
---------------------------
                                 4

SQL> select instr(‘1*3*5*7*9‘,‘*‘) from dual;

INSTR(‘1*3*5*7*9‘,‘*‘)
----------------------
                           2

SQL> select instr(‘1*3*5*7*9‘,‘*‘,2) from dual;

INSTR(‘1*3*5*7*9‘,‘*‘,2)
------------------------
                             2

6. SUBSTR函數(Substring)

SUBSTR函數從給定源字串中給定的位置開始,提取指定長度的子字串。其文法為SUBSTR(source string,start position,[number of characters to extrace])。最後一個參數可選,倘若沒有指定,預設從start position到source string結尾的字元數。

例如:

SQL> select substr(‘1*3*5*7‘,3) from dual;

SUBST
-----
3*5*7

SQL> select substr(‘1*3*5*7‘,3,2) from dual;

SU
--
3*

SQL> select substr(‘1*3*5*7‘,-3,2) from dual;

SU
--
5*

7. REPLACE函數

REPLACE函數用替換項取代源字串中出現的所有搜尋項。其文法為REPLACE(source string,search item,[replacement term])。replacement term預設為空白字元。

例如:

SQL> select replace(‘1*3*5*7‘,‘*‘,‘->‘) from dual;

REPLACE(‘1
----------
1->3->5->7

SQL> select replace(‘1*3*5*7‘,‘*‘) from dual;

REPL
----
1357

三、 數字函數

1. 數字round函數

ROUND函數依據指定的小數精度對數值進行四捨五入運算。其文法為ROUND(source number,decimal precision)。decimal precision參數指定舍入的精度。預設是0,即對源數字四捨五入到整數。如果指定的小數精度n為正數,則要舍入的有效數字在小數點右邊(n+1)個位置。如果n為負數,則要舍入的有效數字在小數點左邊n個位置。

例如:

SQL> select round(145.78,-1) from dual;

ROUND(145.78,-1)
----------------
150

SQL> select round(145.78) from dual;

ROUND(145.78)
-------------
146

SQL> select round(145.78,1) from dual;

ROUND(145.78,1)
---------------
145.8

2. TRUNC函數(Truncate)

TRUNC函數依據指定的小數精度對數值進行截取運算。其文法為TRUNC(source number,decimal precision)。

例如:

SQL> select trunc(145.78,-1) from dual;

TRUNC(145.78,-1)
----------------
140

SQL> select trunc(145.78) from dual;

TRUNC(145.78)
-------------
145

SQL> select trunc(145.78,1) from dual;

TRUNC(145.78,1)
---------------
145.7

3. MOD函數(Modulus)

MOD函數返回除法運算的餘數。

例如:

SQL> select mod(5,2) from dual;

MOD(5,2)
----------
           1

SQL> select mod(5.2,2.2) from dual;

MOD(5.2,2.2)
------------
             .8

四、 日期函數

1. MONTHS_BETWEEN函數

MONTHS_BETWEEN函數返回兩個日期參數之間月數的數值。其文法為MONTHS_BETWEEN(date1,date2)。該Function Computedate1和date2之間月份的差值(每月31天),即date1-date2。傳回值可能由整數和小數部分組成。其中,整數表示這兩個日期之間相差的月數。小數部分表示剩餘的天數和時間,以31天的月份為基礎。

例如:

SQL> select months_between(sysdate,sysdate-31) from dual;

MONTHS_BETWEEN(SYSDATE,SYSDATE-31)
----------------------------------
                                          1

SQL> select months_between(‘2-MAR-2014‘,‘1-FEB-2014‘) from dual;

MONTHS_BETWEEN(‘2-MAR-2014‘,‘1-FEB-2014‘)
-----------------------------------------
                                    1.03225806

SQL> select 1/31 from dual;

1/31
----------
.032258065

--可以看出小數部分是以31天為基礎的。

2. ADD_MONTHS函數

ADD_MONTHS函數文法為ADD_MONTHS(date,number of months),即計算指定月份添加到指定date後的日期。月數可以為負。

例如:

SQL> select add_months(‘1-MAR-2014‘,1) from dual;

ADD_MONTH
---------
01-APR-14

SQL> select add_months(‘1-MAR-2014‘,2.9) from dual;

ADD_MONTH
---------
01-MAY-14

SQL> select add_months(‘1-MAR-2014‘,-2.9) from dual;

ADD_MONTH
---------
01-JAN-14

3. NEXT_DAY函數

NEXT_DAY返回在指定日期後下一次出現星期幾的日期。其文法為NEXT_DAY(start date,day of the week)。Function Compute在start date之後day of the week下一次出現的日期。day of the week可以是字元值或者整數值。其中,字元值至少是星期名稱的前三個字元,例如,星期日可以表示為sun,sund,sunda或者Sunday。對於整數,1表示星期日,2表示星期一,以此類推。

例如:19-SEP-2014是周五

SQL> select next_day(‘19-SEP-2014‘,5) from dual;

NEXT_DAY(
---------
25-SEP-14

SQL> select next_day(‘19-SEP-2014‘,‘tue‘) from dual;

NEXT_DAY(
---------
23-SEP-14

SQL> select next_day(‘19-SEP-2014‘,‘SUNDAY‘) from dual;

NEXT_DAY(
---------
21-SEP-14

4. LAST_DAY函數

LAST_DAY函數提取指定日期所屬的月,並計算該月最後一天的日期。其文法為LAST_DAY(date)。

例如:

SQL> select last_day(‘19-SEP-2014‘) from dual;

LAST_DAY(
---------
30-SEP-14

5. 日期ROUND函數

日期ROUND函數依據指定的日期精度格式對日期進行舍入運算。其文法為ROUND(source date,[date precision format]).source date表示任意日期。date precision format包括世紀(CC),年(YYYY),季度(Q),月(MM),星期(W),日(DD),時(HH)和分(MI)。

如果日部分大於16,則會入到下一個月,否則舍到當月。如果月在1和6之間,則會舍到當年開頭的日期,否則入到下一年開頭的日期。

例如:

查詢的時間為:2014-09-26 04:44:38

SQL> select round(sysdate) day,round(sysdate,‘w‘)week,round(sysdate,‘month‘)month,round(sysdate,‘year‘)year from dual;

DAY          WEEK         MONTH       YEAR
---------     ---------     ---------      ---------
26-SEP-14 29-SEP-14  01-OCT-14  01-JAN-15

因為查詢時間是4點,沒有查過12點,故DAY是當天。因為本月1號是周一,WEEK返回的是查詢時間起,下一個周一的日期,即29-SEP-14。因為查詢時間是26號,超過16號,故MONTH需入到下一個月。因為查詢時間是9月,超過6月,故入到下一年。

6. 日期TRUNC函數

日期TRUNC函數依據指定的日期精度格式對日期進行截取運算。其文法為TRUNC(source date,[date precision format])。date precision format指定截取的精度,預設的截取精度是日。這就意味著將source date的所有時間都設定為00:00:00(00時、00分和00秒)。在月層級上的截取將source date的日期設定為該月的第一天。年層級的截取返回當年開頭的日期。

例如:

SQL> select trunc(sysdate) day,trunc(sysdate,‘w‘)week,trunc(sysdate,‘month‘)month,trunc(sysdate,‘year‘)year from dual;

DAY           WEEK        MONTH      YEAR
---------     ---------     ---------     ---------
26-SEP-14  22-SEP-14 01-SEP-14 01-JAN-14

唯一需要注意的是WEEK,因為本月1號是周一,WEEK返回的是本周周一的日期。

 

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.