oracle 字符串数字求和函数,Oracle中的函数 字符串、数字、日期

--函数:

--字符串:

--initcap 把首字母变成大写

select initcap('&str') from dual;

--upper :大写

select upper('&str') from dual;

--lower :小写

select lower('&str') from dual;

--substr(字符串,截取的开始位置,截取长度) ,截取子字符串,开始位置是1

select 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; --36

select 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; --23

select 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;在天数上加10

select 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_type

select 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_type

select empno,ename,sal,comm,sal + nvl(comm,0) 月薪 from scott.emp order by sal;

--nvl2(n,m,nm):如果n为空,结果为nm,否则为m

column_name/column_no order_type

select empno,ename,sal,comm, nvl2(null,sal +comm,sal) 月薪 from scott.emp order by sal;

--decode:类似java的switch

select deptno, decode(deptno,10,'市场部',20,'研究部',30,'人事部') 部门名 from scott.emp;

--case:类似于java的if

select 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.emp

select rownum ,e.* from scott.emp e;

--常量列

select e.*,1 from scott.emp e;

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值