oracle中常用的單行函式
字串函式:
大小寫轉換函式
將字串全部轉為大寫輸出
select lower('Hello WOrld') from dual;
將字串全部轉為小寫輸出
select upper('Hello WOrld') from dual;
將字串中每個單次的第一個字母轉為大寫輸出其它轉為小寫輸出
處理字串的函式
連線連個字串
select concat('Hello','World') from dual;
計算字串長度
select length('HeloWorld') from dual;
計算一個字串在另一個字串中的位置
select instr('HelloWorld','o') from dual;
計算從右開始第二個“o”所在的位置
select instr('HelloWorld','o',-1,2) from dual;
截斷字串
select substr('HelloWorld',6) from dual;select substr('HelloWorld',6,2) from dual;
select substr('HelloWorld',-5) from dual;
左填充
select lpad(sal,10,'*') from emp;
右填充
select rpad(sal,10,'*') from emp;
函式巢狀:
select lpad(rpad('good',11,'*'),17,'*')from dual;
替換字串
select replace('he love you','he','I') from dual;
截掉字串首尾連續的相同的字元
select trim('H' from 'HelloWorld') from dual;
左截斷
select ltrim('HelloWorld','H') from dual;
右截斷
select rtrim('HelloWorld','d') from dual;
列印字串對應的ascii碼
SQL> select ascii('A') from dual;
列印ascii碼對應的字元
select chr(65) from dual;
數字函式:
select round(45.926,2) from dual;
select round(45.926,0) from dual;
select round(45.926) from dual;
select round(45.926,-1) from dual;
select trunc(45.926,2) from dual;
select trunc(45.926,0) from dual;
select trunc(45.926) from dual;
select trunc(45.926,-1) from dual;
select ceil(3.1415927) from dual; --進位取整
select abs(100),abs(-100) from dual;
select mod(13,4) from dual; --取餘數
select power(2,3) from dual; --2的3次冪
取數字n的符號,大於0返回1,小於0返回-1,等於0返回0
select sign(123),sign(-100),sign(0) from dual;
日期函式:
select round(sysdate+10,'month') from dual;
select round(add_months(sysdate,6),'year') from dual;
select trunc(sysdate+10,'month') from dual;
select trunc(sysdate,'year') from dual;
select months_between('20-FEB-81','17-DEC-80') from dual;
select add_months(sysdate,2) from dual;
select next_day(sysdate,'SAT') from dual;
select last_day(sysdate) from dual;
轉換函式:
select to_char(sysdate,'yyyy-mm-dd') from dual;
select to_char(sysdate,'fmyyyy-mm-dd') from dual;
select to_char(sysdate,'year-month-day') from dual;
select to_char(sysdate,'year-mon-dy') from dual;
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;
select to_char(sysdate,'dd "of" mm') from dual;
select to_char(10,'xx') from dual; --轉換為16進位制
select to_char(sal,'l00,000.00') sal from emp;
select to_char(sal,'l99,999.99') sal from emp;
select to_char(sal,'l999.99') sal from emp;
select to_number('$950.00','l999.99') from dual;
select to_number('9c','xx') from dual; --轉換為10進位制
select to_date('2000-05-01','yyyy-mm-dd') from dual;
函式巢狀,列印yy年和rr年
select
to_char(sysdate,'yyyy') curr_year,
to_char(to_date('05','yy'),'yyyy') yy05,
to_char(to_date('95','yy'),'yyyy') yy95,
to_char(to_date('05','rr'),'yyyy') rr05,
to_char(to_date('95','rr'),'yyyy') rr95
from dual;
通用函式:
nvl : expr1空則expr2,expr1不空則expr1;
* nvl函式兩個形式引數的資料型別要一致
select ename,sal,comm,sal+nvl(comm,0) from emp;
select ename,nvl(to_char(mgr),'No manager') from emp;
nlv2 : expr1空則expr3,expr1不空則expr2
select ename,nvl2(comm,sal+comm,sal) from emp;
nullif : expr1=expr2返回空,expr1!=expr2返回expr1
coalesce(exp1,exp2,exp3,.........) : 從左向右做列舉判斷返回第一個非空表示式的值
條件表示式:
SELECT ename, job, sal,
CASE job WHEN 'CLERK' THEN 1.10*sal
WHEN 'SALESMAN' THEN 1.15*sal
WHEN 'ANALYST' THEN 1.20*sal
ELSE sal END "REVISED_SALARY"
FROM emp;
select ename, job, sal,
decode (job,
'CLERK',1.10*sal,
'SALESMAN',1.15*sal,
'ANALYST',1.20*sal,
sal) "REVISED_SALARY"
from emp;
系統函式:
檢視當前使用者:
select user from dual;
select SYS_CONTEXT('USERENV','SESSION_USER') from dual;
檢視系統時間:
select sysdate from dual;
檢視客戶端IP地址:
SELECT sys_context('USERENV','IP_ADDRESS') FROM DUAL;
檢視主機名:
SELECT sys_context('USERENV','HOST') FROM DUAL;
檢視當前方案:
SELECT sys_context('USERENV','CURRENT_SCHEMA') FROM DUAL;
檢視當前的安全稽核機制:
SELECT sys_context('USERENV','AUTHENTICATION_TYPE') FROM DUAL;