1. 程式人生 > >對表數據的基本操作

對表數據的基本操作

-1 兩個 指定 沒有 har 操作 null 之間 pan


--對日期的做法
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;

對表數據的基本操作