假设一张表有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;