假設一張表有100w條數據,測試單行函數,會返回100w條數據,這樣返回會佔屏幕,很麻煩因此係統提供一個測試表,dual(單行單列的表)sql
select * from dual;
能夠用它進行單行函數的測試(也能夠本身再建一個單行單列的表)shell
select first_name, upper(first_name) from s_emp where id=1;
影響了一行的狀況(id爲1的只有一個):bash
select first_name, upper(first_name) from s_emp where id<1;
一行都沒有影響的狀況(id小於1的不存在):服務器
select first_name, upper(first_name) from s_emp where id>1;
影響多行的狀況(id大於1的有24個):oracle
select lower('HELLO') from dual;
select initcap('one world one dream') from dual;
oncat(par1 varchar2,par2)//把類型寫後面 鏈接字符串
select concat('hello', 'world') from dual;
要是複雜拼接,用concat不方便。函數
select concat(concat('a','b'),concat('c','d')) from dual;
須要函數嵌套(把被嵌套函數的返回值做爲函數的參數使用)測試
select 'a' || 'b' || 'c' || 'd' con from dual;
用|| 就更方便。編碼
substr(par1,par2,par3) 截取字符串
若是非要從0編號,oracle會自動把0變爲1。spa
select substr('hello',0,2) from dual;
select substr('hello',1,2) from dual;
select substr('hello',-3,2) from dual;
select first_name ,substr(first_name,-3,3) from s_emp;
replace(par1,par2,par3)
select replace('one world one dream','one','two') from dual;
nvl(par1,par2) 爲空時替換
round(par1,par2) 四捨五入
四捨五入默認取整:操作系統
select round(9.58) from dual;
保留小數點後兩位四捨五入:
select round(9.486,2) from dual;
對小數點前兩位進行四捨五入取整:
select round(190.486,-2) from dual;
trunc(par1,par2) 截取
select trunc(9.58) from dual; select trunc(9.486,2) from dual; select trunc(190.486,-2) from dual; select trunc(190.486,-1) from dual;
to_number(par1)
因此下面三個結果同樣:
select id, first_name from s_emp where id='1'; select id, first_name from s_emp where id=to_numer('1'); select id, first_name from s_emp where id=1;
to_char(par1,par2) 轉換數字的顯示格式
在小數點後,表明1~9的任意數字
在小數點後面表明0~9的任意數字
演示:
select to_char(12345,'fm$099,999.99') from dual;
select to_char(12345,'fm$099,999.00') from dual;
select to_char(12345.85,'fmL099,999.99') from dual;
select salary , to_char(salary,'fm$000,000.00') from s_emp;
select salary, to_char(salary,'fm$099,999.00') from s_emp;
(1)遠程登陸服務器後,切換shell
bash
(2)打開配置文件
vi .bash_profile
(3)寫入配置
export NLS_LANG=’SIMPLIFIED CHINA.ZHS16GBK’(簡體中文編碼)
export NLS_LANG=’AMERICAN_AMERICA.ZHS16GBK’(美語)
(4)保存退出
Esc+shift+z z
(5)source .bash_profile 讓配置文件生效
(6)從新進入sqlplus
把一個函數的返回值做爲另外一個函數的參數
select first_name, substr(first_name,length(first_name)-2,3);
由於編號是從1開始的,length(first_name)是倒數第一個。
length(first_name)-2就是倒數第三個了
select id, first_name, nvl(to_char(manager_id), 'BOSS');
常見的函數:
select count(*), max(salary), min(salary) from s_emp;
(*只用在count裏面,統計有多少條)
select count(*), avg(salary), sum(salary) from s_emp;
把重複的值去掉以後再統計:
select count(*), avg(distinct salary), sum(distinct salary) from s_emp;
忽略不統計(排序裏把空當作最大值,可是在組函數統計最大值是是忽略空)
例如:25個員工裏,有些人的提成爲空;
select count(commission_pct), sum(commision_pct), avg(commission_pct) from s_emp;