Oracle_SQL(6) 單行函數
一、單行函數
1.定義:對表或視圖的查詢時,針對每行記錄返回一個值的函數。
2.用途:用於select語句,where條件
3.分類:
數值函數 Number Functions
字符函數(返回字符) Character Functions Returning Character Values
字符函數(返回數值) Character Functions Returning Number Values
日期函數 Datetime Functions
轉換函數 Conversion Functions
其它單值函數 Miscellaneous Single-Row Functions
二、數值函數
數值函數:接收一個數值參數,返回一個數值結果的函數。
常用函數:
1.MOD(m,n) m/n取余函數
SELECT MOD(11,4) "Modulus" FROM DUAL;
2.ABS(n) 取絕對值函數
SELECT ABS(-15) "Absolute" FROM DUAL;
3.SIGN(n) 符號函數,正數返回1,負數返回-1,0返回0
SELECT SIGN(-15) "Sign" FROM DUAL;
4.CEIL(n) 向上取整函數
SELECT CEIL(15.7) "Ceiling" FROM DUAL;
5.FLOOR(n) 向下取整函數
SELECT FLOOR(15.7) "Floor" FROM DUAL;
SELECT ROUND(15.193) "Round" FROM DUAL;
SELECT ROUND(15.193,1) "Round" FROM DUAL;
SELECT ROUND(15.193,-1) "Round" FROM DUAL;
7.TRUNC(m)/TRUNC(m,n) 截斷函數
SELECT TRUNC(15.79) "Truncate" FROM DUAL;
SELECT TRUNC(15.79,1) "Truncate" FROM DUAL;
SELECT TRUNC(15.79,-1) "Truncate" FROM DUAL;
1.POWER(m,n) m的n次方函數
SELECT POWER(10,2) "Raised" FROM DUAL;
2.SQRT 平方根函數
SELECT SQRT(25) "Square root" FROM DUAL;
3.LOG(m,n) n的m次方根函數
SELECT LOG(10,100) "Log base 10 of 100" FROM DUAL;
SELECT LOG(10,POWER(10,2)) "Log base 10 of 100" FROM DUAL;
4.EXP e(e = 2.71828183)的m次方函數
SELECT EXP(4) "e to the 4th power" FROM DUAL;
5.LN m的e(e = 2.71828183)次方根函數
SELECT LN(exp(4)) "Natural log of exp(4)" FROM DUAL;
6.SIN 正玄函數
SELECT SIN(30 * 3.14159265359/180) "Sine of 30 degrees" FROM DUAL;
7.COS 余玄函數
SELECT COS(60 * 3.14159265359/180) "Cosine of 60 degrees" FROM DUAL;
8.TAN 正切函數
SELECT TAN(45 * 3.14159265359/180) "Tangent of 45 degrees" FROM DUAL;
9.ASIN 反正玄函數
SELECT ASIN(0.5) "Arc_Sine" FROM DUAL;
SELECT ASIN(SIN(30 * 3.14159265359/180)) "Arc_Sine" FROM DUAL;
10.ACOS 反余玄函數
SELECT ACOS(COS(60 * 3.14159265359/180)) "Arc_Cosine" FROM DUAL;
11.ATAN 反正切函數
SELECT ATAN(TAN(45 * 3.14159265359/180)) "Arc_Tangent" FROM DUAL;
12.ATAN2
SELECT ATAN2(0.3, 0.2) "Arc_Tangent2" FROM DUAL;
13.SINH 雙曲正玄
SELECT SINH(1) "Hyperbolic sine of 1" FROM DUAL;
14.COSH 雙曲余玄函數
SELECT COSH(0) "Hyperbolic cosine of 0" FROM DUAL;
15.TANH 雙曲正切
SELECT TANH(.5) "Hyperbolic tangent of .5" FROM DUAL;
三、字符函數(返回字符) Character Functions Returning Character Values
1.CHR(n) 將數字轉換為ASCII碼
select CHR(65) from dual;
2.CONCAT(m,n) 連接字符串,等同於連接操作符||
select CONCAT(‘A‘,‘B‘) from dual;
select CONCAT(CONCAT(‘A‘,‘B‘),‘C‘) from dual;
select ‘A‘||‘B‘||‘C‘ from dual;
3.INITCAP(n) 將字符中單詞的首字母大寫
SELECT INITCAP(‘hello sql‘) "Capitals" FROM DUAL;
4.UPPER(n) 將字符中字母大寫
SELECT UPPER(‘hello sql‘) "Uppercase" FROM DUAL;
5.LOWER(n) 將字符中字母小寫
SELECT LOWER(‘HELLO SQL‘) "Lowercase" FROM DUAL;
6.LPAD(c1,n,c2) 從左側補齊字符串的長度
SELECT LPAD(‘Page 1‘,15,‘*.‘) "LPAD example" FROM DUAL;
SELECT LPAD(‘Page 1‘,15) "LPAD example" FROM DUAL;
7.RPAD(c1,n,c2) 從右側補齊字符串的長度
SELECT RPAD(‘Page 1‘,15,‘*.‘) "LPAD example" FROM DUAL;
SELECT RPAD(‘Page 1‘,15) "LPAD example" FROM DUAL;
8.TRIM 修剪前後兩端的空格或指定字符
SELECT TRIM (‘ AB C ‘) "TRIM Example" from dual;
--SELECT TRIM (‘00098702348900‘,‘0‘) "TRIM Example" from dual;
SELECT TRIM (0 FROM 00098702348900) "TRIM Example" from dual;
9.LTRIM 修剪前端的空格或指定字符
SELECT LTRIM (‘ AB C ‘) "LTRIM Example" from dual;
SELECT LTRIM (‘00098702348900‘,‘0‘) "LTRIM Example" from dual;
10.RTRIM 修剪後端的空格或指定字符
SELECT RTRIM (‘ AB C ‘) "RTRIM Example" from dual;
SELECT RTRIM (‘00098702348900‘,‘0‘) "RTRIM Example" from dual;
11.SUBSTR(c,p,l) 從字符串指定位置截取指定長度的子字符串
SELECT SUBSTR(‘ABCDEFG‘,3,4) "Substring" FROM DUAL;
SELECT SUBSTR(‘ABCDEFG‘,3) "Substring" FROM DUAL;
SELECT SUBSTR(‘ABCDEFG‘,-3) "Substring" FROM DUAL;
SELECT SUBSTR(‘ABCDEFG‘,-3,4) "Substring" FROM DUAL;
SELECT SUBSTR(‘ABCDEFG‘,1,2) "Substring" FROM DUAL;
SELECT SUBSTR(‘ABCDEFG‘,0,2) "Substring" FROM DUAL;
SELECT SUBSTR(‘我是老師‘,1,4) "Substring" FROM DUAL;
SELECT SUBSTRB(‘我是老師‘,1,4) "Substring" FROM DUAL;
SELECT SUBSTRB(‘我是老師‘,1,3) "Substring" FROM DUAL;
SELECT SUBSTRB(‘A我B是C老F師G‘,1,3) "Substring" FROM DUAL;
SELECT SUBSTRC(‘我是老師‘,1,3) "Substring" FROM DUAL;
SELECT SUBSTR(‘A我B是C老F師G‘,1,3) "Substring" FROM DUAL;
SELECT SUBSTRC(‘A我B是C老F師G‘,1,3) "Substring" FROM DUAL;
12.REPLACE(c,sc,rc) 從c字符串中找到sc子串並用rc子串進行替換
SELECT REPLACE(‘JACK and JUE‘,‘J‘,‘BL‘) "Changes" FROM DUAL;
SELECT REPLACE(‘我是老師‘,‘‘,‘*‘) FROM DUAL;
SELECT REPLACE(‘我是老師‘,‘‘) FROM DUAL;
13.TRANSLATE(c,from_c,to_c) 格式轉化,按照from_c到to_c的模式轉化字符串c
SELECT TRANSLATE(‘2KRW229‘,‘0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ‘,
‘9999999999XXXXXXXXXXXXXXXXXXXXXXXXXX‘) "License" FROM DUAL;
SELECT TRANSLATE(‘2KRW229‘,‘0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ‘,
‘0123456789‘) FROM DUAL;
select TRANSLATE(‘2018‘,‘0123456789‘,‘零一二三四五六七八九‘) from dual;
select TRANSLATE(‘1588‘,‘0123456789‘,‘零壹貳叁肆伍陸柒捌玖‘) from dual;
四、字符函數(返回數值) Character Functions Returning Number Values
1.ASCII(c) 返回字符的ASCII碼
SELECT ASCII(‘A‘) FROM DUAL;
2.INSTR(string,substring,position,occurrence)
查找,返回子字符串的位置
SELECT INSTR(‘CORPORATE FLOOR‘,‘O‘) "Instring" FROM DUAL;
SELECT INSTR(‘CORPORATE FLOOR‘,‘OR‘) "Instring" FROM DUAL;
SELECT INSTR(‘CORPORATE FLOOR‘,‘OR‘,3) "Instring" FROM DUAL;
SELECT INSTR(‘CORPORATE FLOOR‘,‘OR‘,-3) "Instring" FROM DUAL; --位置參數是負數時,從右向左找。
SELECT INSTR(‘CORPORATE FLOOR‘,‘OR‘,3,2) "Instring" FROM DUAL;
SELECT INSTR(‘CORPORATE FLOOR‘,‘OR‘,-3,2) "Instring" FROM DUAL;
select ename,INSTR(ename,‘O‘) ,substr(ename,INSTR(ename,‘O‘)) from emp
3.LENGTH(c) 返回字符串長度
SELECT LENGTH(‘ABCDEFG‘) "Length in characters" FROM DUAL;
SELECT LENGTH(‘我是老師‘) "Length in characters" FROM DUAL;
SELECT LENGTHB(‘ABCDEFG‘) "Length in bytes" FROM DUAL;
SELECT LENGTHB(‘我是老師‘) "Length in bytes" FROM DUAL;
五、日期函數 Datetime Functions
日期 +/- 數字 = 日期
SELECT sysdate+5,sysdate-10 FROM DUAL;
日期 - 日期 = 數字
SELECT sysdate-TO_DATE(‘2017-08-16‘,‘yyyy-mm-dd‘) FROM DUAL;
1.SYSDATE 當前日期 SYSTIMESTAMP、CURRENT_DATE、
CURRENT_TIMESTAMP、LOCALTIMESTAMP
select sysdate from dual;
select sysdate,SYSTIMESTAMP,CURRENT_DATE,
CURRENT_TIMESTAMP,LOCALTIMESTAMP from dual;
2.LAST_DAY(date) 取本月最後1天
select sysdate,last_day(sysdate) from dual;
3.NEXT_DAY(date,n) 獲取下個星期幾的日期
select sysdate,next_day(sysdate,1) from dual;
4.ADD_MONTHS(d,n) 月份加減數字
select sysdate,add_months(sysdate,1),add_months(sysdate,-2),
add_months(sysdate,0.5) from dual;
5.MONTHS_BETWEEN(d1,d2) 月份相減
SELECT MONTHS_BETWEEN(sysdate,TO_DATE(‘2017-08-16‘,‘yyyy-mm-dd‘)) "Months" FROM DUAL;
6.EXTRACT (類型 from date) 從日期中提取年月日時分秒
SELECT EXTRACT(YEAR FROM DATE ‘2017-03-07‘) FROM DUAL;
SELECT EXTRACT(MONTH FROM sysdate) FROM DUAL;
SELECT EXTRACT(DAY FROM sysdate) FROM DUAL;
SELECT EXTRACT(HOUR FROM LOCALTIMESTAMP) FROM DUAL;
SELECT EXTRACT(MINUTE FROM LOCALTIMESTAMP) FROM DUAL;
SELECT EXTRACT(SECOND FROM LOCALTIMESTAMP) FROM DUAL;
7.TRUNC(date) 日期截斷
SELECT TRUNC(sysdate, ‘yyyy‘) "New Year" FROM DUAL;
SELECT TRUNC(sysdate, ‘mm‘) "New Months" FROM DUAL;
SELECT TRUNC(sysdate, ‘dd‘) "New Day" FROM DUAL;
SELECT TRUNC(sysdate, ‘hh24‘) "New Hour" FROM DUAL;
SELECT TRUNC(sysdate, ‘mi‘) "New Minute" FROM DUAL;
8.ROUND(date) 日期四舍五入
SELECT ROUND(sysdate, ‘yyyy‘) "New Year" FROM DUAL;
SELECT ROUND(sysdate, ‘mm‘) "New Year" FROM DUAL;
SELECT ROUND(sysdate, ‘dd‘) "New Year" FROM DUAL;
SELECT ROUND(sysdate, ‘hh24‘) "New Year" FROM DUAL;
SELECT ROUND(sysdate, ‘mi‘) "New Year" FROM DUAL;
六、轉換函數 Conversion Functions
轉換函數:將數值從一個類型轉換到另一個類型。
1.BIN_TO_NUM 二進制轉數字
SELECT BIN_TO_NUM(1,0,1,0) FROM DUAL;
2.TO_CHAR (datetime) 日期轉字符串
SELECT to_char(sysdate,‘yyyy-mm-dd hh24:mi:ss‘) from dual;
3.TO_DATE 字符串轉日期
SELECT sysdate - to_date(‘2017-01-01‘,‘yyyy-mm-dd‘) from dual;
4.TO_NUMBER 字符轉數字
SELECT to_number(‘012345‘) from dual;
SELECT to_number(‘0123.45‘,‘9999D99‘) from dual;
SELECT to_number(‘$0123.45‘,‘$9999D99‘) from dual;
七、其它單值函數 Miscellaneous Single-Row Functions
1.DECODE 條件選擇函數
SELECT emp.*,decode(deptno,10,‘ACCOUNTING‘,
20,‘RESEARCH‘,30,‘SALES‘,‘OPERATIONS‘) from emp;
SELECT emp.*,decode(sign(sal-1600),
1,‘工資大於1600‘,0,‘工資等於1600‘,-1,‘工資小於1600‘) from emp;
SELECT emp.*,decode(sign(instr(ename,‘A‘)),
1,‘名字包含A‘,0,‘名字不包含A‘) from emp;
2.coalesce (expr1, expr2, ..., exprn) 返回首個非空表達式
SELECT emp.*,COALESCE (comm,0) from emp;
3.NVL(c,v) NVL2(c,v1,v2) 空值轉化
SELECT emp.*,sal+comm,sal+nvl(comm,0) from emp;
SELECT emp.*,nvl2(comm,sal+comm,sal) from emp;
4.USER 返回當前用戶名\ID
SELECT USER from dual;
SELECT UID FROM DUAL;
5.SYS_GUID 返回由16字節組成的全局唯一標識符(原始值)。
SELECT SYS_GUID() from dual;
6.NULLIF(s1,s2) s1與s2相等時返回空,不等時返回s1
SELECT emp.deptno,dept.deptno,
nullif(emp.deptno,dept.deptno) from emp,dept;
7.GREATEST 返回集合中最大值
SELECT GREATEST (‘A‘, ‘B‘, ‘C‘) "Greatest" FROM DUAL;
8.LEAST 返回集合中最小值
SELECT LEAST (‘A‘, ‘B‘, ‘C‘) "least" FROM DUAL;
Oracle_SQL(6) 單行函數