Oracle SQL函數

來源:互聯網
上載者:User

標籤:

Oracle將函數大致分為單行函數,彙總函式和分析函數。

單行函數分為字元函數,日期函數,轉換函式,數字函數,通用函數,decode函數

一.字元函數

 1)大小寫控制函數

    01.Lower() 全部小寫

--字元函數--小寫select Lower ( ‘HAPPY‘) from dual;

效果:

   02.Upper()  全部大寫

--大寫select Upper (‘happy‘) from dual;

效果:

   03.initcap()  首字母大寫 

--首字母大寫select initcap (‘happy‘) from dual;

 效果:

2)字元控制函數

     01.concat()   拼接

--拼接select concat(‘happy‘,‘boy‘) from dual;

效果:

     02.substr()   截取字元

--截取字串 位置從1開始select substr(‘happyboy‘,6) from dual;

效果:

--要截取的字元,起始位置,取幾個字元select substr(‘happyboy‘,6,3) from dual;

效果:

    03.length()和lengthb()

  --length(‘字串‘):字元個數統計   -- lengthb(‘字串‘):位元組個數統計select length(‘呵呵‘) 字元數,lengthb(‘呵呵‘) as 位元組數 from dual;

效果:

    04.instr()

--instr(‘大字串‘,‘小字串‘)返回小字串在大字串中出現的位置select instr(‘happy hehe‘,‘he‘,2,2) "Instring" from dual;

select instr(‘happy hehe‘,‘he‘,-2,2) "Reversed Instring" from dual;

效果:

 

select instr(‘happy hehe‘,‘he‘,2,2) "Instring in bytes" from dual;

效果:

    05.lpad()和rpad()

--lpad()和rpad()select lpad(‘happy‘,10,‘*‘) from dual;

效果:

二.日期函數

1)日期函數

     01.兩個日期相差的月數

select MONTHS_BETWEEN(TO_DATE(‘02-02-1995‘,‘MM-DD-YYYY‘),TO_DATE(‘01-01-1995‘,‘MM-DD-YYYY‘)) "Months"from dual;

效果:

    02.向指定日期中加上若干月數

--向指定日期中加上若干月數select TO_CHAR(ADD_MONTHS(hiredate,1),‘DD-MON-YYYY‘) "Next month" from empwhere ENAME=‘JONES‘;

效果:

2)日期相減

    01.兩個日期間的天數

--兩個日期間的天數select floor(sysdate-to_date(‘20020405‘,‘yyyymmdd‘)) from dual;

效果:

    02.兩個日期相差的月數

 

--兩個日期相差的月數select months_between(sysdate,to_date(‘20111204‘,‘yyyymmdd‘)) from dual;

效果:

三。轉換函式

1)隱式轉換

--轉換函式--隱式函數select * from empwhere hiredate=‘17-12月-80‘;

效果:

2)顯示轉換

   01.to_char()對日期的轉換

--顯式函數   --01.to_char()對日期的轉換   select to_char(sysdate,‘yyyy-mm-dd hh24:mi:ss‘) from dual;

效果:

   02.to_char()對數位轉換

--02.to_char()對數位轉換   select to_char(sal,‘L9,999.99‘)    from emp;

效果:

 

四.數字函數

    01.Round()

--數字函數  --01.Round()四捨五入  select round(12.45,1) from dual;

效果:

    02.trunc()截斷

 --02.trunc()截斷  select trunc(15.19,1) "Truncate" from dual;

效果:

 

 五.通用函數

nvl和nvl2濾空函數

   01.nvl濾空函數

select sal*12工資,comm 獎金,sal*12+nvl(comm,0) from emp;

效果:

   02.nvl2濾空函數

select sal*12工資,comm 獎金,sal*12+nvl2(comm,comm,0) from emp;

效果:

 

 六.decode函數

--decode函數  select ename,empno,      decode (ename,‘SMITH‘,1,      ‘ALLEN‘,2,      ‘WARD‘,3,      ‘JONES‘,4) "Location"      from emp      where empno<7600      order by empno,"Location"

效果:

Oracle SQL函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.