Oracle中的函数 字符串、数字、日期

来源:互联网 发布:水化学分析软件 编辑:程序博客网 时间:2024/06/06 10:55
Oracle中的函数操作
--函数:--字符串:--initcap 把首字母变成大写select initcap('&str') from dual;--upper  :大写select upper('&str') from dual;--lower  :小写select lower('&str') from dual;--substr(字符串,截取的开始位置,截取长度) ,截取子字符串,开始位置是1select substr('&str',3,2) from dual;--instr(字符串,查找字符串)  查找字符串,返回初始位置select instr('&str','&substr') from dual;--replace(字符串,要替换的字符串,要替换成的字符串)select instr('hello','l','pp') from dual;--length(字符串) 返回字符串的长度select length('hello') from dual;--rtrim('')  返回字符串的长度,去掉右空格--ltrim('')  返回字符串的长度,去掉左空格select length( ltrim('     hello')), length('     hello')  from dual;--填充空格rpad('', , '*')lpad('', , '*')rpad('hello', 10, '*')--Oracle中的测试表:dual--数字操作:--abs:取绝对值select abs(12) from dual;--floor:向下取整select floor(35.5) from dual;  --35--ceil:向上取整,比当前数大的最小整数select floor(35.5) from dual;  --36--round:四舍五入select round(35.5) from dual;  --36select round(&num) from dual;  --控制台输入select round(35.57878,1) from dual;  --35.6--保留一位小数select round(35.57878,-1) from dual;  --40--trunc:截断truncate 截掉小数select trunc(23.5) from dual; --23select trunc(23.5, 1) from dual; --23--mod:取模,取于select mod(4,3) from dual; --日期操作:--日期 与 日期 计算 == 数值--日期 与 数值 计算 == 日期--sysdate: 当前时间             --给当前列区别名:1、as 别名 --2、"别名" 3、别名select to_char(sysdate,'yyyy-MM-dd, HH:mm:ss day') as "当前日期" from dual;select to_char(sysdate,'yyyy-MM-dd, HH:mm:ss day') "当前日期" from dual;select to_char(sysdate,'yyyy-MM-dd, HH:mm:ss day') 当前日期 from dual;--sysdate + 10;在天数上加10select to_char(sysdate + 10,'yyyy-MM-dd, HH:mm:ss day') as "当前日期" from dual;--add_months(sysdate, n)  :增加月数(日期,增加的月数)select to_char(add_months(sysdate,2),'yyyy-MM-dd, HH:mm:ss day') as "当前日期" from dual;--months_between(d1,d2):两个日期计算 月份的计算 前面的-后面的select months_between(to_date('2013-5-3','yyyy-MM-dd'),to_date('2015-7-3','yyyy-MM-dd')) 两个日期的计算 from dual;--extract (year from sysdate):截取指定日期数值select extract (year from sysdate) 当前的年 from dual;select extract (month from sysdate) 当前的月 from dual;select extract (day from sysdate) 当前的日 from dual;SELECT EXTRACT(TIMEZONE_REGION FROM TIMESTAMP '1999-01-01 10:00:00 -08:00')  FROM DUAL;--last_day:取到当前时间月份的最后一天select last_day(sysdate) from dual ;--next_day(日期,星期n):  --离当前时间最近的没有过的星期n  (星期日~星期六  1~7)select next_day(sysdate,'星期日') from dual ;select next_day(sysdate,1) from dual ;--其他函数:--对员工的工资排序:升序(默认)asc,  降序 desc--排序的语法: order by --column_name/column_no  order_typeselect empno,ename,sal,comm,sal + comm 月薪 from scott.emp order by sal;--数值列如果为空:表示无穷大--nvl(n,m) : 如果n为null ,那么结果就是m, 不是null,结果就是 n。--column_name/column_no  order_typeselect empno,ename,sal,comm,sal + nvl(comm,0) 月薪 from scott.emp order by sal;--nvl2(n,m,nm):如果n为空,结果为nm,否则为mcolumn_name/column_no  order_typeselect empno,ename,sal,comm, nvl2(null,sal +comm,sal) 月薪 from scott.emp order by sal;--decode:类似java的switchselect deptno, decode(deptno,10,'市场部',20,'研究部',30,'人事部') 部门名 from scott.emp;--case:类似于java的ifselect deptno, case deptno when 10 then '市场部' when 20 then '研究部' when 30 then '人事部' else '其他部' end from scott.emp;--统计工资级别(取范围是用<=,>=运算符)select sal, case  when sal<=1000 then '一级工资' when sal<=2000 then '二级工资' when sal<=3000 then '三级工资' else '高级工资' end from scott.emp;--对多条记录得到一个结果的函数就是聚合函数--count:统计条数select count(*) from scott.emp;--如果没有主键,统计所有列找到最大列select count(1) from scott.emp;--常量列,直接统计常量列效率高--sum:计算和select sum(*) from scott.emp; --不可以,要指明要统计的列,并且列的值要是数值类型select sum(sal) from scott.emp; --max:找到最大值select max(sal) 最大工资,min(sal) 最小工资, round(avg(sal),2) 平均工资 from scott.emp;--min:找出最小值select min(sal) 最小工资 from scott.emp;--avg:计算平均值select avg(sal) 平均工资 from scott.emp;--伪列:rownum:Oracle中提供的一个伪列,给行编号,只能从1开始--select rownum ,* from scott.emp;--显示有问题,因为rownum不属于scott.empselect rownum ,e.* from scott.emp e;--常量列select e.*,1 from scott.emp e;

0 0
原创粉丝点击