oracle單行函數,oracle

來源:互聯網
上載者:User

oracle單行函數,oracle


單行函數的文法

Function_name(column|expression, [arg1, arg2, ...])


參數說明:

Function_name    函數名稱

column           列名

expression       運算式

arg1,arg2,...    參數


單行函數分類:

  • 字元函數:接收字元輸入並返回字元或數值

  • 數值函數:接受資料輸入並返回數值

  • 日期函數:對日期型資料進行操作

  • 轉換函式:從一種資料類型轉換為另外一種資料類型

  • 通用函數:NVL 函數 DECODE 函數

字元函數


-- lower() 將字串轉化為小寫select ename from emp where lower(ename) like '%a%';select ename from emp where ename like '%a%'or ename like '%A%'; -- upper() 將字串轉化為大寫select ename from emp where upper(ename) like '%A%';-- initcap() 將單詞的首字母轉為大寫,其餘字母轉為小寫select initcap('HELLO WORLD') from dual;select initcap(ename) from emp;-- concat() 將字串相連select concat('hello', 'world') from dual;select 'hello'||'world' from dual;-- substr() 字串截取select ename, substr(ename, 1, 2), substr(ename, -3, 3) from emp;-- length() 計算長度select length('abcdef'), length('12345678') from dual;-- replace() 字串替換select substr('hello', 3, 2) 取子串, length('hello') 長度, replace('hello', 'l', 'x') from dual;-- chr() 將數字轉為其對應的ascii碼字元select chr(65), chr(66) from dual;-- ascii() 將對應的字元轉為其ascii碼對應的數字select ascii('A'), ascii('B') from dual;


數值函數


-- round() 四捨五入,預設精確到個位,指定精確到小數點後幾位select round(23.652) from dual; select round(23.652, 1) from dual;select round(23.652, -1) from dual;-- trunc() 截斷小數位元,按指定的精度截斷小數或整數(不進行四捨五入運算)select trunc(25.46, 1) from dual;select round(25.46, 1) from dual;select trunc(25.46, -1) from dual;select round(25.46, -1) from dual;-- mod() 對一個數取餘數select mod(10, 3) from dual;


日期函數


Oracle中提供了很多與日期操作相關的函數,主要包括加減。

對日期進行加減運算的時候要遵循一些規則

  日期-數字=日期

  日期+數字=日期

  日期-日期=數字  表示兩個日期之間相隔的天數

-- 顯示10部門僱員進入公司的星期數select empno, ename, deptno, round((sysdate - hiredate)/7) from emp where deptno = 10;-- Months_between() 返回兩個給定日期之間的相隔的月數-- 查詢10部門的僱員工作的月數select deptno, empno, ename, months_between(sysdate, hiredate) from emp where deptno = 10;select deptno, empno, ename, round(months_between(sysdate, hiredate)) from emp where deptno = 10;-- add_months() 返回給定的日期加上指定的月數後的日期select empno, ename, hiredate, add_months(hiredate, 5), add_months(hiredate, -1) from emp;-- next_day() 指定日期下一個指定的星期幾是哪一天select sysdate, next_day(sysdate, '星期一') from dual;-- last_day() 求出給定日期所在月的最後一個日期select last_day(sysdate) from dual;select last_day(to_date('2015-02-13', 'yyyy-mm-dd')) from dual;

轉換函式


to_char()在使用的時候要用到格式控制的符,格式控制符不區分大小寫字母

     年:Y,年份為四位元,故應寫成:YYYY或yyyy

     月:M,月份為兩位元,故應寫成:MM或mm

     日:D,日為兩位元,故應寫成:DD或dd

-- to_char() 將數字或日期轉化為字串-- 對系統的日期格式顯示方式進行轉換,使其按中國日期習慣顯示,即”YYYY-MM-DD”select empno, ename, to_char(hiredate, 'yyyy-mm-dd') from emp;-- fm 去掉個位元的日、月前面的0select empno, ename, to_char(hiredate, 'fmyyyy-mm-dd') from emp;-- 對入職日期按照年、月、日進行拆分select empno,ename,to_char(hiredate,'yyyy') year,       to_char(hiredate,'mm') month,       to_char(hiredate,'dd') dayfrom empselect empno,ename,       to_char(hiredate,'yyyy')||'年'||to_char(hiredate,'mm')||'月'||to_char(hiredate,'dd')||'日'from emp-- to_char() 對數字進行格式化select empno,ename,to_char(sal,'999,999,999') from emp;select empno,ename,to_char(sal,'000,000,000') from emp;select empno,ename,to_char(sal,'$99,999') from emp;-- to_number() 將當前的固定格式的字串轉換為數字select sal from emp where sal>to_number('$1,250.00','$9,999.99');select to_number('300') + to_number('400') from dual;-- to_date() 將當前固定格式的字串轉化為日期-- 查詢1981-1-1 以後入職的員工select ename, hiredate from empwhere hiredate >= to_date('1981-1-1', 'yyyy-mm-dd');select ename, hiredate from empwhere hiredate >= date '1981-1-1';


通用函數


decode(),此函數有類似於If...elseif...else 語句,用於對多分支的判斷

文法: decode(col/expression,search1,result1[,search2,result2,……][,default])

說明:

     Col/expression:為列名或運算式

     Search1,search2……searchI:為多種可能出現的條件

     Result1,result2……resulti:當滿足對應的shearch後為返回值

-- nvl() 對為空白值的欄位進行處理-- 求每個僱員的年薪(包括獎金)select empno, ename, (sal+comm)*12 from emp;select empno,ename,sal,nvl(comm,0) new_comm,(sal+nvl(comm,0))*12 incom from emp;-- docode() 分支判斷select decode(2,1,'內容1',2,'內容2',3,'內容3') from dual;select  empno 僱員編號,ename 僱員姓名,   decode(job,'CLERK','業務員','SALESMAN','銷售人員',              'MANAGER','經理','ANALYST','分析師','PRESIDENT','總裁') 工作from emp


友情連結: oracle簡單查詢,限定查詢及排序


相關文章

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.