首页 > 代码库 > (四)Oracle学习笔记—— 常见函数
(四)Oracle学习笔记—— 常见函数
1. 字符串类型及函数
字符类型分 3 种,char(n) 、varchar(n)、varchar2(n) ;char(n)固定长度字符串,假如长度不足 n,右边空格补齐;varchar(n)可变长度字符串,假如长度不足 n,右边不会补齐;varchar2(n)可变长度字符串,Oracle 官方推荐使用,向后兼容性好;char(n) VS varchar2(n)char(n)查询效率相对较高,varchar2(n)存储空间相对较小;select length(userName) as charlength,length(password) as varchar2length from t_user3;lpad() 向左补全字符串:select lpad(stuno,6,‘0‘) from t_user3;rpad() 向右补全字符串:select rpad(stuno,6,‘0‘) from t_user3;lower() 返回字符串小写:select lower(userName) from t_user3;upper() 返回字符串大写:select upper(userName) from t_user3;initcap() 单词首字符大写:select initcap(userName) from t_user3;length() 返回字符串长度:select length(password) from t_user3;substr() 截取字符串:select substr(userName,1,2) from t_user3;instr() 获取字符串出现的位置:select instr(password,‘23‘,2,2) from t_user3;ltrim() 删除左侧空格:select ltrim(userName) from t_user3;rtrim() 删除右侧空格:select rtrim(userName) from t_user3;trim() 删除两侧空格:select trim(userName) from t_user3;concat() 串联字符串:select concat(userName,password) from t_user3;reverse() 反转字符串:select reverse(userName) from t_user3;
2. 数值类型及函数
number 是 oracle 中的数据类型;number(precision,scale);Precision,scale 均可选;Precision 代表精度,sacle 代表小数位的位数;Precision 范围[1,38] scale 范围[-84,127]举例: 12345.678 Precision 是 8scale 是 3;常用方法:abs() 求绝对值;select abs(n1) from t_number where id=1;round() 四舍五入:select round(n1,2) from t_number where id=1;ceil() 向上取整:select ceil(n1) from t_number where id=2;floor 向下取整:select floor(n1) from t_number where id=2;Mod()取模:select mod(5,3) from dual;Sign()正负性:select sign(n1) from t_number where id=1;Sqrt() 求平方根:select sqrt(9) from dual;Power()求乘方:select power(2,3) from dual;Trunc()截取:select trunc(123.456,2) from dual;To_char() 格式化数值:常见的字符匹配有0、9、,、$、FM、L、Cselect to_char(123.45,‘0000.000‘) from dual;select to_char(123.45,‘9999.999‘) from dual;select to_char(123123,‘99,999,999.99‘) from dual;select to_char(123123.3,‘FM99,999,999.99‘) from dual;select to_char(123123.3,‘$99,999,999.99‘) from dual;select to_char(123123.3,‘L99,999,999.99‘) from dual;select to_char(123123.3,‘99,999,999.99C‘) from dual;
3. 日期类型及函数
Date 和 timestamp(时间戳)Date 包含信息 century(世纪信息) year 年 month 月 day 日 hour 小时 minute 分钟 second 秒Timestamp 一般用于日期时间要求非常精确的情况,精确到毫秒级;insert into t_date values(1,sysdate,systimestamp);下面重点讲 date 类型的常用函数:select sysdate from dual;select systimestamp from dual;Add_months 添加月份 select add_months(d1,2) from t_date where id=1;Last_day 返回指定日期月份的最后一天 select last_day(d1) from t_date where id=1;update t_date set d3=to_date(‘2016-12-20‘,‘YYYY-MM-DD‘) where id=1;update t_date set d3=to_date(‘2016-12-20 18:31:34‘,‘YYYY-MM-DD HH24:MI:SS‘) where id=1;Months_between 返回两个日期的相差月数 select months_between(d1,d3) from t_date where id=1;next_day 返回特定日期之后的一周内的日期:select next_day(d1,2) from t_date where id=1;Trunc 截取日期:select trunc(d1,‘YYYY‘) from t_date where id=1;select trunc(d1,‘MM‘) from t_date where id=1;select trunc(d1,‘DD‘) from t_date where id=1;select trunc(d1,‘HH‘) from t_date where id=1;select trunc(d1,‘MI‘) from t_date where id=1;Extract 返回日期的某个域:select extract(year from sysdate) from dual;select extract(month from sysdate) from dual;select extract(day from sysdate) from dual;select extract(Hour from systimestamp) from dual;select extract(minute from systimestamp) from dual;select extract(second from systimestamp) from dual;To_char 将日期转换成字符串:select to_char(d1,‘YYYY-MM-DD‘) from t_date where id=1;select to_char(d1,‘YYYY-MM-DD HH24:MI:SS‘) from t_date where id=1;
4. 其他常用处理函数
常用的聚合函数:Max 求最大值:select max(sal) from emp;Min 求最小值:select min(sal) from emp;Avg 求平均值:select avg(sal) from emp;Sum 求和:select sum(sal) from emp ;Count 统计记录数:select count(ename) from emp ;Nvl 空值处理:select ename,nvl(sal,0) from emp;rownumOracle 分页:select * from (select a.*,rownum rn from (select * from emp) A where rownum<=10) where rn>5;Oracle 中的运算:select 2+1 from dual;select 2-1 from dual;select 2*1 from dual;select 2/1 from dual;条件判断式:Between and 范围查询:select * from emp where sal between 900 and 1500;select * from emp where sal>=900 and sal<=1500;In 集合范围:select ename,hiredate from emp where ename in (select distinct ename from bonus)Like 模糊查询:select * from emp where ename like ‘%M%‘select * from emp where ename like ‘M%‘select * from emp where ename like ‘_M%‘
(四)Oracle学习笔记—— 常见函数
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。