set linesize 120
set pagesize 20
column file_name format a8
v$nls_parameters 数据字典中的一个表
关于null的注意:
1:包含null的表达式结果为空
2:判断一个值是否等于null是用 is 不等于用is not 。记住(null!=null)
3:如果集合中含有null值,不能使用not in 但可以使用 in
sql 优化:
oracle9之后, *和列名的效率一样,在之前,用*列名效率比较高,因为oracle要解析*,所以效率比较慢
like的使用, 一个_代表匹配所有,多个_代表匹配多个字符,以S开头的 'S%',以S结尾的'%S',包含S的'%S%'
转移字符:escape 后面自顶一个转义字符
查询名字中含有_这个的
1 select *
2 from emp
3* where ename like '%\_%' escape '\'
使用and 时,应该尽量是假值在左边,使用or时应该尽量是真值在左边
排序:
如果要排序的值中含有空值,应当使其在后面 select * from emp order by comm desc nulls last;
修改系统日期的格式
select * from v$bls_parameters;
alter session set NLS_DATE_FORMAT='yyyy-mm-dd';原系统日期格式为'DD-MON-RR'
DISTINCT 去掉重复记录(作用它后面所有的列)
select DISTINCT deptno from emp;
单行函数:
字符函数
lower('Hello Word') 转小写 upper('Hello World') 转大写 select lower('Hello World'),upper('Hello World') from dual
substr(a,b)从a中,第b位开始取,取右边所有的字符 select substr('Hello World',3) from dual;
substr(a,b,c)从a中,第b位开始取,去的长度为c ,select substr('Hello World',2,3) from dual;
length('中国') 字符数 lengthb('中国')字节数 select length('中国'),lengthb('中国') from dual;
instr(a,b)从a中查找b,找到返回下标,否则返回0 select instr('Hello Word','ll') from dual;
lpad左填充 rpad右填充 select lpad('abcd',10,'*') from dual 把字符串abcd填充到第10位,用*填充
trim去掉前后指定的字符 select trim('H' from 'Hello WorldH') from dual; 意思是从 Hello WorldH 去掉H
replace 替换 select replace('hello world',2,'l','*') from dual; 把hello world 中所有字符l用*替换
数字函数
round(45.926,2)四舍五入函数,后面的数字为保留的小数位数 select round(45.926,2) ,round(45.926,1),round(45.926,0),round(45.926,-1),round(45.926,-2) from dual
trunc(45.926,2)一律舍去保留小数位后面的值,不管是否大于5还是小于4 select trunc(45.926,2) ,trunc(45.926,1),trunc(45.926,0),trunc(45.926,-1),trunc(45.926,-2) from dual
mod(1600,300) 求余 select mod(1600,300) from dual;
日期函数
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual; 秒级
select to_char(systimestamp,'yyyy-mm-dd hh24:mi:ss:ff') from dual; 毫秒级
昨天,今天,明天
select (sysdate-1), sysdate, (sysdate+1) from dual;
不允许日期+日期
months_between 两个日期相差的月数 select ename,months_between(sysdate,hiredate) from emp;
add_months 指定日期加上若干月数 select add_months(sysdate,100) from dual;
next_day 指定日期的下一个日期 select next_day(sysdate,'星期三') from dual;
last_day 日期所在月份的最后一天 select last_day(sysdate) from dual
round 日期四射五入 select round(sysdate,'month'), round(sysdate,'year'),round(sysdate,'day') from dual;
trunc 日期截断 select trunc (sysdate,'month'), trunc (sysdate,'year'),trunc (sysdate,'day') from dual;
转换函数
隐性转换(不需要你做什么,oracle数据库自动为你转换,前提是被转换对象是可以转换的)
显性转换(需要自己手动的转换)
显示当前时间 select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss "今天是" day') from dual;
把某一个数字按一定格式转换成字符串
查询员工薪水:两位小数,货比代码,千位符
select to_char(sal,'9,999.99L') from emp;