对表数据的基本操作


--对日期的做法
insert into wan values ('ktf',5000,to_date('1980-08-11 14:40:23 ','yyyy-mm-dd hh24:mi:ss'));
select sal,sal AS saly from emp;
--求某列的平均值
select * from emp;
--
select sal AS nb from emp;
--排序 默认升序 asc 就近原则
select * from emp order by empno,sal;
--降序 desc
select * from emp order by sal desc;
--concat 连接 ||
select concat('hello','nihao') from dual;
select empno,empno+25||' is a '||job AS ta from emp;
--把该字段全部变为小写
select lower(ename) from emp;
--首字母大写
select initcap(ename) from emp;
--全部大写
select upper(ename) from emp;
--substr 选取指定位置 dual 虚表
select substr('hello',2,5) from dual;
--instr 指定字母的位置
select instr('hkjhdkk','j') from dual;
--替换指定的字母
select replace('jlkdj','j','') from dual;
--trim 去掉前后空格
select trim(' derf ') from dual;
--取余 mod ; round 四舍五入 round(222.325,2) 数字,保留位数

select mod(254,12) from dual;
--截断 trunc 负数表示整数部分
select trunc(5125.2545,-2) from dual;
--获取当前日期
select sysdate from dual;
select * from emp;
--日期可以相加减
select round((sysdate-hiredate)/7,1) from emp;
--选择日期在之间的数值 以降序排出
select * from emp
where hiredate between '02-1月-81' and '02-1月-91' order by hiredate desc;
--用每个* 东西在前面补够8位 lpan 前面补; rpan 后面补
select lpad(ename,8,'*')ename from emp;
--用现在的日期减去所选日期 日期只能减
select (sysdate-hiredate)/365 from emp;
--两个日期相差的月数
select months_between(hiredate,sysdate) from emp;
--向指定日期加上若干个月
select add_months(hiredate,34) from emp;
--last_day 本月的最后一天
select last_day(hiredate) from emp;
--系统日期 四舍五入年 、月 不能日
select round(sysdate,'year') from dual;
select round(sysdate,'month') from dual;
--截取年、月 不能日
select hiredate,trunc(hiredate,'year') from emp;
select hiredate,trunc(hiredate,'month') from emp;
select hiredate,add_months(hiredate,7) from emp;
--替换
select sal,replace(sal,'1','5') from emp;
--next_day 指定日期的下一天
select next_day(sysdate,'星期一') from dual;
--以固定格式输出
select to_char(sysdate,'yyyy') from dual;
select to_char(sysdate,'yyyy-mm-dd') from dual;
--输出数字以固定格式
select to_char(sal,'999,999,999') from emp;
--返回日期 周天是1
select sysdate,to_char(sysdate,'d') from dual;
--返回数字
select to_number(12*12+25) from dual;
--以固定格式输出指定的日期
Select to_date('2015126','yyyyMMdd') from dual;
--返回表的行数(元组) 如果表没有数据则返回为null 而不是0
select count(*) from emp;
select count(*) from dept;
select * from emp;
--找出每月倒数第几天
select * from emp where last_day(hiredate)-2=hiredate;
--排序sal 降序
select * from emp order by (sal) desc;

原文地址:https://www.cnblogs.com/xuekai/p/7241494.html