1.过滤和排序

 SQL> --查询10号部门的所有员工信息
SQL> select *
2 from emp
3 where empno = 10; 未选定行 SQL> ed
SP2-0110: 无法创建保存文件 "afiedt.buf"
SQL> 3
3* where empno = 10
SQL> c /empno/deptno;
3* where deptno = 10
SQL> / EMPNO ENAME JOB MGR HIREDATE
---------- -------------------- ------------------ ---------- --------------
SAL COMM DEPTNO
---------- ---------- ----------
7782 CLARK MANAGER 7839 09-6月 -81
2450 10 7839 KING PRESIDENT 17-11月-81
5000 10 7934 MILLER CLERK 7782 23-1月 -82
1300 10 SQL> set linesize 120l
SP2-0268: linesize 选项的编号无效
SQL> set linesize 120;
SQL> / EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- -------------- ---------- ---------- ----------
7782 CLARK MANAGER 7839 09-6月 -81 2450 10
7839 KING PRESIDENT 17-11月-81 5000 10
7934 MILLER CLERK 7782 23-1月 -82 1300 10 SQL> -- 查询名字为KING的员工信息
SQL> select *
2 from emp
3 where ename = 'KING'; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- -------------- ---------- ---------- ----------
7839 KING PRESIDENT 17-11月-81 5000 10 SQL> 3
3* where ename = 'KING'
SQL> c /KING/King;
3* where ename = 'King'
SQL> / 未选定行 SQL> --在oracle中字段的名称是不区分大小写的,但是字段里面的值是区分大小的
SQL> --查询入职日期是81-11-17号的员工信息
SQL> select *
2 from emp
3 where hiredate = '17-11月-81'; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- -------------- ---------- ---------- ----------
7839 KING PRESIDENT 17-11月-81 5000 10 SQL> -- 在oracle中时间的表示格式是 DD-MON-RR
SQL> select * from
2 emp
3 where hiredate = '1981-11-17';
where hiredate = '1981-11-17'
*
第 3 行出现错误:
ORA-01861: 文字与格式字符串不匹配 SQL> --查询oracle中的一般常量设置
SQL> select * from v$nls_paramters;
select * from v$nls_paramters
*
第 1 行出现错误:
ORA-00942: 表或视图不存在 SQL> select * from v$nls_parameters; PARAMETER
------------------------------------------------------------------------------------------------------------------------
VALUE
------------------------------------------------------------------------------------------------------------------------
NLS_LANGUAGE
SIMPLIFIED CHINESE NLS_TERRITORY
CHINA NLS_CURRENCY
¥ PARAMETER
------------------------------------------------------------------------------------------------------------------------
VALUE
------------------------------------------------------------------------------------------------------------------------
NLS_ISO_CURRENCY
CHINA NLS_NUMERIC_CHARACTERS
., NLS_CALENDAR
GREGORIAN PARAMETER
------------------------------------------------------------------------------------------------------------------------
VALUE
------------------------------------------------------------------------------------------------------------------------
NLS_DATE_FORMAT
DD-MON-RR NLS_DATE_LANGUAGE
SIMPLIFIED CHINESE NLS_CHARACTERSET
AL32UTF8 PARAMETER
------------------------------------------------------------------------------------------------------------------------
VALUE
------------------------------------------------------------------------------------------------------------------------
NLS_SORT
BINARY NLS_TIME_FORMAT
HH.MI.SSXFF AM NLS_TIMESTAMP_FORMAT
DD-MON-RR HH.MI.SSXFF AM PARAMETER
------------------------------------------------------------------------------------------------------------------------
VALUE
------------------------------------------------------------------------------------------------------------------------
NLS_TIME_TZ_FORMAT
HH.MI.SSXFF AM TZR NLS_TIMESTAMP_TZ_FORMAT
DD-MON-RR HH.MI.SSXFF AM TZR NLS_DUAL_CURRENCY
¥ PARAMETER
------------------------------------------------------------------------------------------------------------------------
VALUE
------------------------------------------------------------------------------------------------------------------------
NLS_NCHAR_CHARACTERSET
AL16UTF16 NLS_COMP
BINARY NLS_LENGTH_SEMANTICS
BYTE PARAMETER
------------------------------------------------------------------------------------------------------------------------
VALUE
------------------------------------------------------------------------------------------------------------------------
NLS_NCHAR_CONV_EXCP
FALSE 已选择19行。 SQL> set linesize 200;
SQL> / PARAMETER
--------------------------------------------------------------------------------------------------------------------------------
VALUE
--------------------------------------------------------------------------------------------------------------------------------
NLS_LANGUAGE
SIMPLIFIED CHINESE NLS_TERRITORY
CHINA NLS_CURRENCY
¥ PARAMETER
--------------------------------------------------------------------------------------------------------------------------------
VALUE
--------------------------------------------------------------------------------------------------------------------------------
NLS_ISO_CURRENCY CHINA NLS_NUMERIC_CHARACTERS
., NLS_CALENDAR
GREGORIAN PARAMETER
--------------------------------------------------------------------------------------------------------------------------------
VALUE
--------------------------------------------------------------------------------------------------------------------------------
NLS_DATE_FORMAT
DD-MON-RR NLS_DATE_LANGUAGE
SIMPLIFIED CHINESE NLS_CHARACTERSET
AL32UTF8 PARAMETER
--------------------------------------------------------------------------------------------------------------------------------
VALUE
--------------------------------------------------------------------------------------------------------------------------------
NLS_SORT
BINARY NLS_TIME_FORMAT
HH.MI.SSXFF AM NLS_TIMESTAMP_FORMAT
DD-MON-RR HH.MI.SSXFF AM PARAMETER
--------------------------------------------------------------------------------------------------------------------------------
VALUE
--------------------------------------------------------------------------------------------------------------------------------
NLS_TIME_TZ_FORMAT
HH.MI.SSXFF AM TZR NLS_TIMESTAMP_TZ_FORMAT
DD-MON-RR HH.MI.SSXFF AM TZR NLS_DUAL_CURRENCY
¥ PARAMETER
--------------------------------------------------------------------------------------------------------------------------------
VALUE
--------------------------------------------------------------------------------------------------------------------------------
NLS_NCHAR_CHARACTERSET
AL16UTF16 NLS_COMP
BINARY NLS_LENGTH_SEMANTICS
BYTE PARAMETER
--------------------------------------------------------------------------------------------------------------------------------
VALUE
--------------------------------------------------------------------------------------------------------------------------------
NLS_NCHAR_CONV_EXCP
FALSE 已选择19行。 SQL> col parameter for a50;
SQL> / PARAMETER VALUE
-------------------------------------------------- --------------------------------------------------------------------------------------------------------------------------------
NLS_LANGUAGE SIMPLIFIED CHINESE
NLS_TERRITORY CHINA
NLS_CURRENCY ¥
NLS_ISO_CURRENCY CHINA
NLS_NUMERIC_CHARACTERS .,
NLS_CALENDAR GREGORIAN
NLS_DATE_FORMAT DD-MON-RR
NLS_DATE_LANGUAGE SIMPLIFIED CHINESE
NLS_CHARACTERSET AL32UTF8
NLS_SORT BINARY
NLS_TIME_FORMAT HH.MI.SSXFF AM PARAMETER VALUE
-------------------------------------------------- --------------------------------------------------------------------------------------------------------------------------------
NLS_TIMESTAMP_FORMAT DD-MON-RR HH.MI.SSXFF AM
NLS_TIME_TZ_FORMAT HH.MI.SSXFF AM TZR
NLS_TIMESTAMP_TZ_FORMAT DD-MON-RR HH.MI.SSXFF AM TZR
NLS_DUAL_CURRENCY ¥
NLS_NCHAR_CHARACTERSET AL16UTF16
NLS_COMP BINARY
NLS_LENGTH_SEMANTICS BYTE
NLS_NCHAR_CONV_EXCP FALSE 已选择19行。 SQL> alter session set NLS_DATE_FORMAT = 'yyyy-mm-dd'; 会话已更改。 SQL> select *
2 from emp
3 where hiredate='1981-11-17'; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- ---------- ---------- ---------- ----------
7839 KING PRESIDENT 1981-11-17 5000 10 SQL> select *
2 from emp
3 where hiredate='17-11月-81';
where hiredate='17-11月-81'
*
第 3 行出现错误:
ORA-01861: 文字与格式字符串不匹配 SQL> select * from v$nls_parameters; PARAMETER VALUE
-------------------------------------------------- --------------------------------------------------------------------------------------------------------------------------------
NLS_LANGUAGE SIMPLIFIED CHINESE
NLS_TERRITORY CHINA
NLS_CURRENCY ¥
NLS_ISO_CURRENCY CHINA
NLS_NUMERIC_CHARACTERS .,
NLS_CALENDAR GREGORIAN
NLS_DATE_FORMAT yyyy-mm-dd
NLS_DATE_LANGUAGE SIMPLIFIED CHINESE
NLS_CHARACTERSET AL32UTF8
NLS_SORT BINARY
NLS_TIME_FORMAT HH.MI.SSXFF AM PARAMETER VALUE
-------------------------------------------------- --------------------------------------------------------------------------------------------------------------------------------
NLS_TIMESTAMP_FORMAT DD-MON-RR HH.MI.SSXFF AM
NLS_TIME_TZ_FORMAT HH.MI.SSXFF AM TZR
NLS_TIMESTAMP_TZ_FORMAT DD-MON-RR HH.MI.SSXFF AM TZR
NLS_DUAL_CURRENCY ¥
NLS_NCHAR_CHARACTERSET AL16UTF16
NLS_COMP BINARY
NLS_LENGTH_SEMANTICS BYTE
NLS_NCHAR_CONV_EXCP FALSE 已选择19行。 SQL> select *
2 from emp
3 where ename='KING' and job='PRESIDENT'; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- ---------- ---------- ---------- ----------
7839 KING PRESIDENT 1981-11-17 5000 10 SQL> --查询工资在2000-5000之间的所有员工
SQL> select *
2 from emp
3 where sal>2000 and sal < 5000; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- ---------- ---------- ---------- ----------
7566 JONES MANAGER 7839 1981-04-02 2975 20
7698 BLAKE MANAGER 7839 1981-05-01 2850 30
7782 CLARK MANAGER 7839 1981-06-09 2450 10
7788 SCOTT ANALYST 7566 1987-04-19 3000 20
7902 FORD ANALYST 7566 1981-12-03 3000 20 SQL> select *
2 from emp
3 where sal between 2000 and 5000; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- ---------- ---------- ---------- ----------
7566 JONES MANAGER 7839 1981-04-02 2975 20
7698 BLAKE MANAGER 7839 1981-05-01 2850 30
7782 CLARK MANAGER 7839 1981-06-09 2450 10
7788 SCOTT ANALYST 7566 1987-04-19 3000 20
7839 KING PRESIDENT 1981-11-17 5000 10
7902 FORD ANALYST 7566 1981-12-03 3000 20 已选择6行。 SQL> select *
2 from emp
3 where sal between 5000 and 2000; 未选定行 SQL> --between ...and 只能将小的写在前面大的写在后面
SQL> -- in/not in
SQL> --查询20?30号部门的所有员工
SQL> select *
2 from emp
3 where deptno = 20 or deptno = 30; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- ---------- ---------- ---------- ----------
7369 SMITH CLERK 7902 1980-12-17 800 20
7499 ALLEN SALESMAN 7698 1981-02-20 1600 300 30
7521 WARD SALESMAN 7698 1981-02-22 1250 500 30
7566 JONES MANAGER 7839 1981-04-02 2975 20
7654 MARTIN SALESMAN 7698 1981-09-28 1250 1400 30
7698 BLAKE MANAGER 7839 1981-05-01 2850 30
7788 SCOTT ANALYST 7566 1987-04-19 3000 20
7844 TURNER SALESMAN 7698 1981-09-08 1500 0 30
7876 ADAMS CLERK 7788 1987-05-23 1100 20
7900 JAMES CLERK 7698 1981-12-03 950 30
7902 FORD ANALYST 7566 1981-12-03 3000 20 已选择11行。 SQL> select *
2 from emp
3 where deptno in(20,30); EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- ---------- ---------- ---------- ----------
7369 SMITH CLERK 7902 1980-12-17 800 20
7499 ALLEN SALESMAN 7698 1981-02-20 1600 300 30
7521 WARD SALESMAN 7698 1981-02-22 1250 500 30
7566 JONES MANAGER 7839 1981-04-02 2975 20
7654 MARTIN SALESMAN 7698 1981-09-28 1250 1400 30
7698 BLAKE MANAGER 7839 1981-05-01 2850 30
7788 SCOTT ANALYST 7566 1987-04-19 3000 20
7844 TURNER SALESMAN 7698 1981-09-08 1500 0 30
7876 ADAMS CLERK 7788 1987-05-23 1100 20
7900 JAMES CLERK 7698 1981-12-03 950 30
7902 FORD ANALYST 7566 1981-12-03 3000 20 已选择11行。 SQL> -- 查询不在20和30号部门里面的所有员工
SQL> select *
2 from emp
3 where deptno != 20 and deptno != 30; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- ---------- ---------- ---------- ----------
7782 CLARK MANAGER 7839 1981-06-09 2450 10
7839 KING PRESIDENT 1981-11-17 5000 10
7934 MILLER CLERK 7782 1982-01-23 1300 10 SQL> select *
2 from emp
3 where deptno not in(20,30); EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- ---------- ---------- ---------- ----------
7782 CLARK MANAGER 7839 1981-06-09 2450 10
7839 KING PRESIDENT 1981-11-17 5000 10
7934 MILLER CLERK 7782 1982-01-23 1300 10 SQL> -- 查询奖金为300和为空的所有员工信息
SQL> select *
2 from emp
3 where comm in(300,null); EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- ---------- ---------- ---------- ----------
7499 ALLEN SALESMAN 7698 1981-02-20 1600 300 30 SQL> select *
2 from emp
3 where comm not in (300,null); 未选定行 SQL> -- in里面可以跟null,而not in 里面不能跟null
SQL> -- 查询comm为空的所有员工信息
SQL> select *
2 from emp
3 where comm is null; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- ---------- ---------- ---------- ----------
7369 SMITH CLERK 7902 1980-12-17 800 20
7566 JONES MANAGER 7839 1981-04-02 2975 20
7698 BLAKE MANAGER 7839 1981-05-01 2850 30
7782 CLARK MANAGER 7839 1981-06-09 2450 10
7788 SCOTT ANALYST 7566 1987-04-19 3000 20
7839 KING PRESIDENT 1981-11-17 5000 10
7876 ADAMS CLERK 7788 1987-05-23 1100 20
7900 JAMES CLERK 7698 1981-12-03 950 30
7902 FORD ANALYST 7566 1981-12-03 3000 20
7934 MILLER CLERK 7782 1982-01-23 1300 10 已选择10行。 SQL> -- 模糊查询
SQL> -- 查询员工名字里面包含了O的所有员工信息
SQL> select *
2 from emp
3 where ename like '%O%'; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- ---------- ---------- ---------- ----------
7566 JONES MANAGER 7839 1981-04-02 2975 20
7788 SCOTT ANALYST 7566 1987-04-19 3000 20
7902 FORD ANALYST 7566 1981-12-03 3000 20 SQL> -- like 是一个关键字 表示模糊查询,其中的% 代表通配
SQL> -- 查询员工名字里面以K打头的员工信息
SQL> select *
2 from emp
3 where ename like 'K%'; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- ---------- ---------- ---------- ----------
7839 KING PRESIDENT 1981-11-17 5000 10 SQL> -- 查询ename的长度为4的所有员工信息
SQL> select *
2 from emp
3 where ename like '____'; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- ---------- ---------- ---------- ----------
7521 WARD SALESMAN 7698 1981-02-22 1250 500 30
7839 KING PRESIDENT 1981-11-17 5000 10
7902 FORD ANALYST 7566 1981-12-03 3000 20 SQL> -- 在oracle中_ 代表一个字符
SQL> insert into emp(empno,ename,sal,deptno) values(1001,'TOM_ab',50000,20); 已创建 1 行。 SQL> select * from emp; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- ---------- ---------- ---------- ----------
7369 SMITH CLERK 7902 1980-12-17 800 20
7499 ALLEN SALESMAN 7698 1981-02-20 1600 300 30
7521 WARD SALESMAN 7698 1981-02-22 1250 500 30
7566 JONES MANAGER 7839 1981-04-02 2975 20
7654 MARTIN SALESMAN 7698 1981-09-28 1250 1400 30
7698 BLAKE MANAGER 7839 1981-05-01 2850 30
7782 CLARK MANAGER 7839 1981-06-09 2450 10
7788 SCOTT ANALYST 7566 1987-04-19 3000 20
7839 KING PRESIDENT 1981-11-17 5000 10
7844 TURNER SALESMAN 7698 1981-09-08 1500 0 30
7876 ADAMS CLERK 7788 1987-05-23 1100 20 EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- ---------- ---------- ---------- ----------
7900 JAMES CLERK 7698 1981-12-03 950 30
7902 FORD ANALYST 7566 1981-12-03 3000 20
7934 MILLER CLERK 7782 1982-01-23 1300 10
1001 TOM_ab 50000 20 已选择15行。 SQL> -- 查询ename字段里面包含了_的员工信息
SQL> select *
2 from emp
3 where ename like '%_%'; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- ---------- ---------- ---------- ----------
7369 SMITH CLERK 7902 1980-12-17 800 20
7499 ALLEN SALESMAN 7698 1981-02-20 1600 300 30
7521 WARD SALESMAN 7698 1981-02-22 1250 500 30
7566 JONES MANAGER 7839 1981-04-02 2975 20
7654 MARTIN SALESMAN 7698 1981-09-28 1250 1400 30
7698 BLAKE MANAGER 7839 1981-05-01 2850 30
7782 CLARK MANAGER 7839 1981-06-09 2450 10
7788 SCOTT ANALYST 7566 1987-04-19 3000 20
7839 KING PRESIDENT 1981-11-17 5000 10
7844 TURNER SALESMAN 7698 1981-09-08 1500 0 30
7876 ADAMS CLERK 7788 1987-05-23 1100 20 EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- ---------- ---------- ---------- ----------
7900 JAMES CLERK 7698 1981-12-03 950 30
7902 FORD ANALYST 7566 1981-12-03 3000 20
7934 MILLER CLERK 7782 1982-01-23 1300 10
1001 TOM_ab 50000 20 已选择15行。 SQL> -- oracle中的转义
SQL> select *
2 from emp
3 where ename like '%\_%' escape '\'; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- ---------- ---------- ---------- ----------
1001 TOM_ab 50000 20 SQL> select * from emp; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- ---------- ---------- ---------- ----------
7369 SMITH CLERK 7902 1980-12-17 800 20
7499 ALLEN SALESMAN 7698 1981-02-20 1600 300 30
7521 WARD SALESMAN 7698 1981-02-22 1250 500 30
7566 JONES MANAGER 7839 1981-04-02 2975 20
7654 MARTIN SALESMAN 7698 1981-09-28 1250 1400 30
7698 BLAKE MANAGER 7839 1981-05-01 2850 30
7782 CLARK MANAGER 7839 1981-06-09 2450 10
7788 SCOTT ANALYST 7566 1987-04-19 3000 20
7839 KING PRESIDENT 1981-11-17 5000 10
7844 TURNER SALESMAN 7698 1981-09-08 1500 0 30
7876 ADAMS CLERK 7788 1987-05-23 1100 20 EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- ---------- ---------- ---------- ----------
7900 JAMES CLERK 7698 1981-12-03 950 30
7902 FORD ANALYST 7566 1981-12-03 3000 20
7934 MILLER CLERK 7782 1982-01-23 1300 10
1001 TOM_ab 50000 20 已选择15行。 SQL> rollback; 回退已完成。 SQL> / 回退已完成。 SQL> select * from emp; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- ---------- ---------- ---------- ----------
7369 SMITH CLERK 7902 1980-12-17 800 20
7499 ALLEN SALESMAN 7698 1981-02-20 1600 300 30
7521 WARD SALESMAN 7698 1981-02-22 1250 500 30
7566 JONES MANAGER 7839 1981-04-02 2975 20
7654 MARTIN SALESMAN 7698 1981-09-28 1250 1400 30
7698 BLAKE MANAGER 7839 1981-05-01 2850 30
7782 CLARK MANAGER 7839 1981-06-09 2450 10
7788 SCOTT ANALYST 7566 1987-04-19 3000 20
7839 KING PRESIDENT 1981-11-17 5000 10
7844 TURNER SALESMAN 7698 1981-09-08 1500 0 30
7876 ADAMS CLERK 7788 1987-05-23 1100 20 EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- ---------- ---------- ---------- ----------
7900 JAMES CLERK 7698 1981-12-03 950 30
7902 FORD ANALYST 7566 1981-12-03 3000 20
7934 MILLER CLERK 7782 1982-01-23 1300 10 已选择14行。 SQL> spool off;

  

 SQL> /*
SQL> sql 优化2:
SQL> where条件优化:
SQL> 执行的顺序是从右往左去执行 where deptno = 10 and ename like '%a%'; or
SQL> */
SQL> -- 排序 order by
SQL> -- 根据员工的sal 排序
SQL> select *
2 from emp
3 order by sal; EMPNO ENAME JOB MGR HIREDATE
---------- -------------------- ------------------ ---------- --------------
SAL COMM DEPTNO
---------- ---------- ----------
7369 SMITH CLERK 7902 17-12月-80
800 20 7900 JAMES CLERK 7698 03-12月-81
950 30 7876 ADAMS CLERK 7788 23-5月 -87
1100 20 EMPNO ENAME JOB MGR HIREDATE
---------- -------------------- ------------------ ---------- --------------
SAL COMM DEPTNO
---------- ---------- ----------
7521 WARD SALESMAN 7698 22-2月 -81
1250 500 30 7654 MARTIN SALESMAN 7698 28-9月 -81
1250 1400 30 7934 MILLER CLERK 7782 23-1月 -82
1300 10 EMPNO ENAME JOB MGR HIREDATE
---------- -------------------- ------------------ ---------- --------------
SAL COMM DEPTNO
---------- ---------- ----------
7844 TURNER SALESMAN 7698 08-9月 -81
1500 0 30 7499 ALLEN SALESMAN 7698 20-2月 -81
1600 300 30 7782 CLARK MANAGER 7839 09-6月 -81
2450 10 EMPNO ENAME JOB MGR HIREDATE
---------- -------------------- ------------------ ---------- --------------
SAL COMM DEPTNO
---------- ---------- ----------
7698 BLAKE MANAGER 7839 01-5月 -81
2850 30 7566 JONES MANAGER 7839 02-4月 -81
2975 20 7788 SCOTT ANALYST 7566 19-4月 -87
3000 20 EMPNO ENAME JOB MGR HIREDATE
---------- -------------------- ------------------ ---------- --------------
SAL COMM DEPTNO
---------- ---------- ----------
7902 FORD ANALYST 7566 03-12月-81
3000 20 7839 KING PRESIDENT 17-11月-81
5000 10 已选择14行。 SQL> set linesize 200;
SQL> / EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- -------------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17-12月-80 800 20
7900 JAMES CLERK 7698 03-12月-81 950 30
7876 ADAMS CLERK 7788 23-5月 -87 1100 20
7521 WARD SALESMAN 7698 22-2月 -81 1250 500 30
7654 MARTIN SALESMAN 7698 28-9月 -81 1250 1400 30
7934 MILLER CLERK 7782 23-1月 -82 1300 10
7844 TURNER SALESMAN 7698 08-9月 -81 1500 0 30
7499 ALLEN SALESMAN 7698 20-2月 -81 1600 300 30
7782 CLARK MANAGER 7839 09-6月 -81 2450 10
7698 BLAKE MANAGER 7839 01-5月 -81 2850 30
7566 JONES MANAGER 7839 02-4月 -81 2975 20 EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- -------------- ---------- ---------- ----------
7788 SCOTT ANALYST 7566 19-4月 -87 3000 20
7902 FORD ANALYST 7566 03-12月-81 3000 20
7839 KING PRESIDENT 17-11月-81 5000 10 已选择14行。 SQL> --默认情况下oarcle是按照升序排列
SQL> select *
2 from emp
3 order by sal desc; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- -------------- ---------- ---------- ----------
7839 KING PRESIDENT 17-11月-81 5000 10
7902 FORD ANALYST 7566 03-12月-81 3000 20
7788 SCOTT ANALYST 7566 19-4月 -87 3000 20
7566 JONES MANAGER 7839 02-4月 -81 2975 20
7698 BLAKE MANAGER 7839 01-5月 -81 2850 30
7782 CLARK MANAGER 7839 09-6月 -81 2450 10
7499 ALLEN SALESMAN 7698 20-2月 -81 1600 300 30
7844 TURNER SALESMAN 7698 08-9月 -81 1500 0 30
7934 MILLER CLERK 7782 23-1月 -82 1300 10
7521 WARD SALESMAN 7698 22-2月 -81 1250 500 30
7654 MARTIN SALESMAN 7698 28-9月 -81 1250 1400 30 EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- -------------- ---------- ---------- ----------
7876 ADAMS CLERK 7788 23-5月 -87 1100 20
7900 JAMES CLERK 7698 03-12月-81 950 30
7369 SMITH CLERK 7902 17-12月-80 800 20 已选择14行。 SQL> select *
2 from emp
3 order by sal asc; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- -------------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17-12月-80 800 20
7900 JAMES CLERK 7698 03-12月-81 950 30
7876 ADAMS CLERK 7788 23-5月 -87 1100 20
7521 WARD SALESMAN 7698 22-2月 -81 1250 500 30
7654 MARTIN SALESMAN 7698 28-9月 -81 1250 1400 30
7934 MILLER CLERK 7782 23-1月 -82 1300 10
7844 TURNER SALESMAN 7698 08-9月 -81 1500 0 30
7499 ALLEN SALESMAN 7698 20-2月 -81 1600 300 30
7782 CLARK MANAGER 7839 09-6月 -81 2450 10
7698 BLAKE MANAGER 7839 01-5月 -81 2850 30
7566 JONES MANAGER 7839 02-4月 -81 2975 20 EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- -------------- ---------- ---------- ----------
7788 SCOTT ANALYST 7566 19-4月 -87 3000 20
7902 FORD ANALYST 7566 03-12月-81 3000 20
7839 KING PRESIDENT 17-11月-81 5000 10 已选择14行。 SQL> -- desc 降序 asc升序
SQL> -- 按照sal和部门号排序
SQL> select *
2 from emp
3 order by sal,deptno; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- -------------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17-12月-80 800 20
7900 JAMES CLERK 7698 03-12月-81 950 30
7876 ADAMS CLERK 7788 23-5月 -87 1100 20
7521 WARD SALESMAN 7698 22-2月 -81 1250 500 30
7654 MARTIN SALESMAN 7698 28-9月 -81 1250 1400 30
7934 MILLER CLERK 7782 23-1月 -82 1300 10
7844 TURNER SALESMAN 7698 08-9月 -81 1500 0 30
7499 ALLEN SALESMAN 7698 20-2月 -81 1600 300 30
7782 CLARK MANAGER 7839 09-6月 -81 2450 10
7698 BLAKE MANAGER 7839 01-5月 -81 2850 30
7566 JONES MANAGER 7839 02-4月 -81 2975 20 EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- -------------- ---------- ---------- ----------
7788 SCOTT ANALYST 7566 19-4月 -87 3000 20
7902 FORD ANALYST 7566 03-12月-81 3000 20
7839 KING PRESIDENT 17-11月-81 5000 10 已选择14行。 SQL> select *
2 from emp
3 order by job,deptno; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- -------------- ---------- ---------- ----------
7902 FORD ANALYST 7566 03-12月-81 3000 20
7788 SCOTT ANALYST 7566 19-4月 -87 3000 20
7934 MILLER CLERK 7782 23-1月 -82 1300 10
7369 SMITH CLERK 7902 17-12月-80 800 20
7876 ADAMS CLERK 7788 23-5月 -87 1100 20
7900 JAMES CLERK 7698 03-12月-81 950 30
7782 CLARK MANAGER 7839 09-6月 -81 2450 10
7566 JONES MANAGER 7839 02-4月 -81 2975 20
7698 BLAKE MANAGER 7839 01-5月 -81 2850 30
7839 KING PRESIDENT 17-11月-81 5000 10
7654 MARTIN SALESMAN 7698 28-9月 -81 1250 1400 30 EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- -------------------- ------------------ ---------- -------------- ---------- ---------- ----------
7844 TURNER SALESMAN 7698 08-9月 -81 1500 0 30
7521 WARD SALESMAN 7698 22-2月 -81 1250 500 30
7499 ALLEN SALESMAN 7698 20-2月 -81 1600 300 30 已选择14行。 SQL> -- 如果排序后面跟了多个列,那么会首先使用第一列来排,第一列拍出来的,使用第二列来拍,以此类推。
SQL> -- order by 后面能跟什么东西?
SQL> -- order by 列明,别名,表达式,列所在的数字顺序
SQL> select deptno,empno 员工号
2 from emp
3 order by 员工号; DEPTNO 员工号
---------- ----------
20 7369
30 7499
30 7521
20 7566
30 7654
30 7698
10 7782
20 7788
10 7839
30 7844
20 7876 DEPTNO 员工号
---------- ----------
30 7900
20 7902
10 7934 已选择14行。 SQL> select sal*12 基本工资
2 from emp
3 order by sal*12; 基本工资
----------
9600
11400
13200
15000
15000
15600
18000
19200
29400
34200
35700 基本工资
----------
36000
36000
60000 已选择14行。 SQL> select empno,deptno,sal*12 + nvl(comm,0)
2 from emp
3 order by 3; EMPNO DEPTNO SAL*12+NVL(COMM,0)
---------- ---------- ------------------
7369 20 9600
7900 30 11400
7876 20 13200
7521 30 15500
7934 10 15600
7654 30 16400
7844 30 18000
7499 30 19500
7782 10 29400
7698 30 34200
7566 20 35700 EMPNO DEPTNO SAL*12+NVL(COMM,0)
---------- ---------- ------------------
7788 20 36000
7902 20 36000
7839 10 60000 已选择14行。 SQL> select empno,deptno
2 from emp
3 order by empno,depton desc;
order by empno,depton desc
*
第 3 行出现错误:
ORA-00904: "DEPTON": 标识符无效 SQL> 3
3* order by empno,depton desc
SQL> c /depton/deptno;
3* order by empno,deptno desc
SQL> / EMPNO DEPTNO
---------- ----------
7369 20
7499 30
7521 30
7566 20
7654 30
7698 30
7782 10
7788 20
7839 10
7844 30
7876 20 EMPNO DEPTNO
---------- ----------
7900 30
7902 20
7934 10 已选择14行。 SQL> select job,deptno
2 from emp
3 order by job,deptno desc; JOB DEPTNO
------------------ ----------
ANALYST 20
ANALYST 20
CLERK 30
CLERK 20
CLERK 20
CLERK 10
MANAGER 30
MANAGER 20
MANAGER 10
PRESIDENT 10
SALESMAN 30 JOB DEPTNO
------------------ ----------
SALESMAN 30
SALESMAN 30
SALESMAN 30 已选择14行。 SQL> -- desc只用用于理他最近的那一列
SQL> select empno,comm
2 from emp
3 order by comm desc; EMPNO COMM
---------- ----------
7369
7782
7902
7900
7876
7566
7698
7934
7788
7839
7654 1400 EMPNO COMM
---------- ----------
7521 500
7499 300
7844 0 已选择14行。 SQL> -- 在oracle中null最大
SQL> -- 让null在最后显示
SQL> select empno,comm
2 from emp
3 order by comm desc
4 nulls last; EMPNO COMM
---------- ----------
7654 1400
7521 500
7499 300
7844 0
7788
7839
7876
7900
7902
7934
7698 EMPNO COMM
---------- ----------
7566
7369
7782 已选择14行。 SQL> spool off;

2.单行函数

  

 SQL> -- 大写 小写 首字母大写
SQL> select upper('hello world') 大写, lower('Hello World') 小写,initcap('hello world') 首字母大写 from dual; 大写 小写 首字母大写
---------------------- ---------------------- ----------------------
HELLO WORLD hello world Hello World SQL> -- 字节和字符的统计
SQL> select length('中国') 字符,lengthb('中国') 字节 from dual; 字符 字节
---------- ----------
2 6 SQL> -- 替换字符串里面的o
SQL> select replace('hello','o','a') from dual; REPLACE('H
----------
hella SQL> select replace('hello','o') from dual; REPLACE(
--------
hell SQL> select lengthb(replace('hello','o')) from dual; LENGTHB(REPLACE('HELLO','O'))
-----------------------------
4 SQL> -- 截取字符串的子串
SQL> select substr('hello world',3) from dual; SUBSTR('HELLOWORLD
------------------
llo world SQL> select substr('hello world',3,4) from dual; SUBSTR('
--------
llo SQL> /*
SQL> substr('',a) 从第a为开始截取到最后
SQL> substr('',a,b) 从第a位开始截取截取b个长度
SQL> */
SQL> -- 在一个字符串中查找某一个字符或者字符串
SQL> select instr('hello world','o') from dual; INSTR('HELLOWORLD','O')
-----------------------
5 SQL> select instr('hello world','hello') from dual; INSTR('HELLOWORLD','HELLO')
---------------------------
1 SQL> -- instr 查询第一次出现某一个字符或者字符串的位置
SQL> -- 左补齐和右补齐
SQL> select lpad('hello world',15,'*') 左补齐,rpad('hello world',15,'@') 右补齐 from dual; 左补齐 右补齐
------------------------------ ------------------------------
****hello world hello world@@@@ SQL> -- 去掉字符串里面的某一个字符或者子串
SQL> select trim('hello world','hel') from dual;
select trim('hello world','hel') from dual
*
第 1 行出现错误:
ORA-00907: 缺失右括号 SQL> select trim('hel' from 'hello world') from dual;
select trim('hel' from 'hello world') from dual
*
第 1 行出现错误:
ORA-30001: 截取集仅能有一个字符 SQL> select trim('h' from 'hello world') from dual; TRIM('H'FROM'HELLOWO
--------------------
ello world SQL> -- trim只能去除一个字符,不能去除字符串
SQL> -- 数值函数
SQL> -- round 四舍五入
SQL> select round(42.965,2) 一,round(42.965,1) 二,round(42.965,0) 三,
2 round(42.965,-1) 四,round(42.965,-2) 五 from dual; 一 二 三 四 五
---------- ---------- ---------- ---------- ----------
42.97 43 43 40 0 SQL> select trunc(42.965,2) 一,trunc(42.965,1) 二,trunc(42.965,0) 三,
2 trunc(42.965,-1) 四,trunc(42.965,-2) 五 from dual; 一 二 三 四 五
---------- ---------- ---------- ---------- ----------
42.96 42.9 42 40 0 SQL> select mod(10,3) from dual; MOD(10,3)
----------
1 SQL> select mod(3,10) from dual; MOD(3,10)
----------
3 SQL> spool off
 SQL> -- 查询系统当前时间
SQL> select sysdate from dual; SYSDATE
--------------
15-3月 -16 SQL> alter session set NLS_DATE_PARAMETER='yyyy-mm-dd';
alter session set NLS_DATE_PARAMETER='yyyy-mm-dd'
*
第 1 行出现错误:
ORA-00922: 选项缺失或无效 SQL> select * from v$nls_parameters; PARAMETER
--------------------------------------------------------------------------------------------------------------------------------
VALUE
--------------------------------------------------------------------------------------------------------------------------------
NLS_LANGUAGE
SIMPLIFIED CHINESE NLS_TERRITORY
CHINA NLS_CURRENCY
¥ PARAMETER
--------------------------------------------------------------------------------------------------------------------------------
VALUE
--------------------------------------------------------------------------------------------------------------------------------
NLS_ISO_CURRENCY
CHINA NLS_NUMERIC_CHARACTERS
., NLS_CALENDAR
GREGORIAN PARAMETER
--------------------------------------------------------------------------------------------------------------------------------
VALUE
--------------------------------------------------------------------------------------------------------------------------------
NLS_DATE_FORMAT
DD-MON-RR NLS_DATE_LANGUAGE
SIMPLIFIED CHINESE NLS_CHARACTERSET
AL32UTF8 PARAMETER
--------------------------------------------------------------------------------------------------------------------------------
VALUE
--------------------------------------------------------------------------------------------------------------------------------
NLS_SORT
BINARY NLS_TIME_FORMAT
HH.MI.SSXFF AM NLS_TIMESTAMP_FORMAT
DD-MON-RR HH.MI.SSXFF AM PARAMETER
--------------------------------------------------------------------------------------------------------------------------------
VALUE
--------------------------------------------------------------------------------------------------------------------------------
NLS_TIME_TZ_FORMAT
HH.MI.SSXFF AM TZR NLS_TIMESTAMP_TZ_FORMAT
DD-MON-RR HH.MI.SSXFF AM TZR NLS_DUAL_CURRENCY
¥ PARAMETER
--------------------------------------------------------------------------------------------------------------------------------
VALUE
--------------------------------------------------------------------------------------------------------------------------------
NLS_NCHAR_CHARACTERSET
AL16UTF16 NLS_COMP
BINARY NLS_LENGTH_SEMANTICS
BYTE PARAMETER
--------------------------------------------------------------------------------------------------------------------------------
VALUE
--------------------------------------------------------------------------------------------------------------------------------
NLS_NCHAR_CONV_EXCP
FALSE 已选择19行。 SQL> set linesize 200;
SQL> / PARAMETER
--------------------------------------------------------------------------------------------------------------------------------
VALUE
--------------------------------------------------------------------------------------------------------------------------------
NLS_LANGUAGE
SIMPLIFIED CHINESE NLS_TERRITORY
CHINA NLS_CURRENCY
¥ PARAMETER
--------------------------------------------------------------------------------------------------------------------------------
VALUE
--------------------------------------------------------------------------------------------------------------------------------
NLS_ISO_CURRENCY
CHINA NLS_NUMERIC_CHARACTERS
., NLS_CALENDAR
GREGORIAN PARAMETER
--------------------------------------------------------------------------------------------------------------------------------
VALUE
--------------------------------------------------------------------------------------------------------------------------------
NLS_DATE_FORMAT
DD-MON-RR NLS_DATE_LANGUAGE
SIMPLIFIED CHINESE NLS_CHARACTERSET
AL32UTF8 PARAMETER
--------------------------------------------------------------------------------------------------------------------------------
VALUE
--------------------------------------------------------------------------------------------------------------------------------
NLS_SORT
BINARY NLS_TIME_FORMAT
HH.MI.SSXFF AM NLS_TIMESTAMP_FORMAT
DD-MON-RR HH.MI.SSXFF AM PARAMETER
--------------------------------------------------------------------------------------------------------------------------------
VALUE
--------------------------------------------------------------------------------------------------------------------------------
NLS_TIME_TZ_FORMAT
HH.MI.SSXFF AM TZR NLS_TIMESTAMP_TZ_FORMAT
DD-MON-RR HH.MI.SSXFF AM TZR NLS_DUAL_CURRENCY
¥ PARAMETER
--------------------------------------------------------------------------------------------------------------------------------
VALUE
--------------------------------------------------------------------------------------------------------------------------------
NLS_NCHAR_CHARACTERSET
AL16UTF16 NLS_COMP
BINARY NLS_LENGTH_SEMANTICS
BYTE PARAMETER
--------------------------------------------------------------------------------------------------------------------------------
VALUE
--------------------------------------------------------------------------------------------------------------------------------
NLS_NCHAR_CONV_EXCP
FALSE 已选择19行。 SQL> col parameter for a50;
SQL> / PARAMETER VALUE
-------------------------------------------------- --------------------------------------------------------------------------------------------------------------------------------
NLS_LANGUAGE SIMPLIFIED CHINESE
NLS_TERRITORY CHINA
NLS_CURRENCY ¥
NLS_ISO_CURRENCY CHINA
NLS_NUMERIC_CHARACTERS .,
NLS_CALENDAR GREGORIAN
NLS_DATE_FORMAT DD-MON-RR
NLS_DATE_LANGUAGE SIMPLIFIED CHINESE
NLS_CHARACTERSET AL32UTF8
NLS_SORT BINARY
NLS_TIME_FORMAT HH.MI.SSXFF AM PARAMETER VALUE
-------------------------------------------------- --------------------------------------------------------------------------------------------------------------------------------
NLS_TIMESTAMP_FORMAT DD-MON-RR HH.MI.SSXFF AM
NLS_TIME_TZ_FORMAT HH.MI.SSXFF AM TZR
NLS_TIMESTAMP_TZ_FORMAT DD-MON-RR HH.MI.SSXFF AM TZR
NLS_DUAL_CURRENCY ¥
NLS_NCHAR_CHARACTERSET AL16UTF16
NLS_COMP BINARY
NLS_LENGTH_SEMANTICS BYTE
NLS_NCHAR_CONV_EXCP FALSE 已选择19行。 SQL> alter session set NLS_DATE_FORMAT='yyyy-mm-dd'; 会话已更改。 SQL> select sysdate from dual; SYSDATE
----------
2016-03-15 SQL> -- 改变时间的显示的另一种方式
SQL> select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual; TO_CHAR(SYSDATE,'YYYY-MM-DDHH24:MI:SS'
--------------------------------------
2016-03-15 16:39:38 SQL> desc emp;
名称 是否为空? 类型
----------------------------------------------------------------------------------------------------------------- -------- ----------------------------------------------------------------------------
EMPNO NOT NULL NUMBER(4)
ENAME VARCHAR2(10)
JOB VARCHAR2(9)
MGR NUMBER(4)
HIREDATE DATE
SAL NUMBER(7,2)
COMM NUMBER(7,2)
DEPTNO NUMBER(2) SQL> -- oracle中如果你的字符串的格式满足日期的格式系统会根据你的需求帮你转换
SQL> -- 输出昨天 ,今天 和明天
SQL> select sysdate + 1 明天,sysdate 今天, sysdate - 1 昨天 from dual; 明天 今天 昨天
---------- ---------- ----------
2016-03-16 2016-03-15 2016-03-14 SQL> -- 明天减去今天等于多少
SQL> select (sysdate + 1) - (sysdate) from dual; (SYSDATE+1)-(SYSDATE)
---------------------
1 SQL> -- 明天加今天等于多少
SQL> select (sydate + 1) + sysdate from dual;
select (sydate + 1) + sysdate from dual
*
第 1 行出现错误:
ORA-00904: "SYDATE": 标识符无效 SQL> -- 日期不能去做加法,只能加几天,可以做减法,减去一个日期或者几天
SQL> -- 根据入司时间计算一下emp表里面的所有员工的工龄
SQL> select (sysdate - hiredate) 天,(sysdate -hiredate)/7 周, (sysdate - hiredate)/30 月 from emp; 天 周 月
---------- ---------- ----------
12872.7023 1838.95747 429.090077
12807.7023 1829.67176 426.92341
12805.7023 1829.38604 426.856743
12766.7023 1823.81461 425.556743
12587.7023 1798.24319 419.590077
12737.7023 1819.67176 424.590077
12698.7023 1814.10033 423.290077
10558.7023 1508.38604 351.956743
12537.7023 1791.10033 417.92341
12607.7023 1801.10033 420.256743
10524.7023 1503.5289 350.82341 天 周 月
---------- ---------- ----------
12521.7023 1788.81461 417.390077
12521.7023 1788.81461 417.390077
12470.7023 1781.5289 415.690077 已选择14行。 SQL> select (sysdate - hiredate)/30 粗略, months_between(sysdate,hiredate) 精确 from emp; 粗略 精确
---------- ----------
429.090142 422.958202
426.923475 420.861427
426.856808 420.796911
425.556808 419.442073
419.590142 413.603363
424.590142 418.474331
423.290142 417.216266
351.956808 346.893685
417.923475 411.958202
420.256808 414.248524
350.823475 345.764653 粗略 精确
---------- ----------
417.390142 411.409814
417.390142 411.409814
415.690142 409.764653 已选择14行。 SQL> -- 在一个日期上加上几个月
SQL> select add_months(sysdate,5) from dual; ADD_MONTHS
----------
2016-08-15 SQL> -- 查看当前月的最后一天
SQL> select last_day(sysdate) from dual; LAST_DAY(S
----------
2016-03-31 SQL> -- 下一个日期 比如下一个星期六
SQL> select next_day(sysdate,'星期二') from dual; NEXT_DAY(S
----------
2016-03-22 SQL> -- 日期的四舍五入
SQL> select round(sysdate,'year') from dual; ROUND(SYSD
----------
2016-01-01 SQL> select round(sysdate,'month') from dual; ROUND(SYSD
----------
2016-03-01 SQL> select round(sysdate,'day') from dual; ROUND(SYSD
----------
2016-03-13 SQL> select sysdate from dual; SYSDATE
----------
2016-03-15 SQL> select trunc(sysdate,'day') from dual; TRUNC(SYSD
----------
2016-03-13 SQL> -- 今天是星期二 天气真的很好
SQL> select to_char(sysdate,'"今天是"yyyy-mm-dd day "天气真的很好") from dual;
ERROR:
ORA-01756: 引号内的字符串没有正确结束 SQL> select to_char(sysdate,'"今天是"yyyy-mm-dd day "天气真的很好"') from dual; TO_CHAR(SYSDATE,'"今天是"YYYY-MM-DDDAY"天气真的很好"')
------------------------------------------------------------------------------------------------------
今天是2016-03-15 星期二 天气真的很好 SQL> -- 双引号的作用除了作为别名以外还可以作为日期格式里面的常亮
SQL> --常量
SQL> -- 将整数转换为一个字符串
SQL> select to_char(1524251,L999,999) from dual;
select to_char(1524251,L999,999) from dual
*
第 1 行出现错误:
ORA-00904: "L999": 标识符无效 SQL> select to_char(1524251,'L999,999') from dual; TO_CHAR(1524251,'L999,999')
------------------------------------
################## SQL> select to_char(1524251,'L9,999,999') from dual; TO_CHAR(1524251,'L9,999,999')
----------------------------------------
¥1,524,251 SQL> -- L 表示本地货比
SQL> select to_number('12345','99,99') from dual;
select to_number('12345','99,99') from dual
*
第 1 行出现错误:
ORA-01722: 无效数字 SQL> select to_number('12345','99,999') from dual
2 ;
select to_number('12345','99,999') from dual
*
第 1 行出现错误:
ORA-01722: 无效数字 SQL> select to_number('123') from dual; TO_NUMBER('123')
----------------
123 SQL> -- 通用函数
SQL> -- nvl2 是nvl的一个加强版
SQL> -- nvl2(a,b,c) 如果a==null ,返回c否则返回b
SQL> select sal,nvl2(comm,comm,sal) from emp; SAL NVL2(COMM,COMM,SAL)
---------- -------------------
800 800
1600 300
1250 500
2975 2975
1250 1400
2850 2850
2450 2450
3000 3000
5000 5000
1500 0
1100 1100 SAL NVL2(COMM,COMM,SAL)
---------- -------------------
950 950
3000 3000
1300 1300 已选择14行。 SQL> select sal,nvl2(comm,comm,sal), comm from emp; SAL NVL2(COMM,COMM,SAL) COMM
---------- ------------------- ----------
800 800
1600 300 300
1250 500 500
2975 2975
1250 1400 1400
2850 2850
2450 2450
3000 3000
5000 5000
1500 0 0
1100 1100 SAL NVL2(COMM,COMM,SAL) COMM
---------- ------------------- ----------
950 950
3000 3000
1300 1300 已选择14行。 SQL> -- nullif(a,b) 如果a == b 返回空,如果不等于返回a
SQL> select nullif('abc','abc') from dual; NULLIF
------ SQL> select nullif('abc','abcc') from dual; NULLIF
------
abc SQL> -- coalesce(a,b,c....) 从最左边开始查找,查找到第一个不为空的返回
SQL> select sal, comm, coalesce(sal,comm,empno) from emp; SAL COMM COALESCE(SAL,COMM,EMPNO)
---------- ---------- ------------------------
800 800
1600 300 1600
1250 500 1250
2975 2975
1250 1400 1250
2850 2850
2450 2450
3000 3000
5000 5000
1500 0 1500
1100 1100 SAL COMM COALESCE(SAL,COMM,EMPNO)
---------- ---------- ------------------------
950 950
3000 3000
1300 1300 已选择14行。 SQL> select sal, comm, coalesce(comm,sal,empno) from emp; SAL COMM COALESCE(COMM,SAL,EMPNO)
---------- ---------- ------------------------
800 800
1600 300 300
1250 500 500
2975 2975
1250 1400 1400
2850 2850
2450 2450
3000 3000
5000 5000
1500 0 0
1100 1100 SAL COMM COALESCE(COMM,SAL,EMPNO)
---------- ---------- ------------------------
950 950
3000 3000
1300 1300 已选择14行。 SQL> -- case 和decode ,case是sql99标准里面的内容,decode只有在oracle中支持
SQL> select sal 调前,
2 case job
3 when 'PRESIDENT' then sal + 1000;
when 'PRESIDENT' then sal + 1000
*
第 3 行出现错误:
ORA-00905: 缺失关键字 SQL> when 'MANAGER' then sal + 800;
SP2-0734: 未知的命令开头 "when 'MANA..." - 忽略了剩余的行。
SQL> sal + 400;
SP2-0042: 未知命令 "sal + 400" - 其余行忽略。
SQL> end 涨后 from emp;
SP2-0734: 未知的命令开头 "end 涨后 f..." - 忽略了剩余的行。
SQL>
SQL>
SQL> select sal 调前,case job when 'PRESIDENT' then sal + 1000;when 'MANAGER' then sal + 800;sal + 400;end 涨后 from emp;
select sal 调前,case job when 'PRESIDENT' then sal + 1000;when 'MANAGER' then sal + 800;sal + 400;end 涨后 from emp
*
第 1 行出现错误:
ORA-00911: 无效字符 SQL> select sal, case
2 when job = 'PRESIDENT' then sal + 1000;
when job = 'PRESIDENT' then sal + 1000
*
第 2 行出现错误:
ORA-00905: 缺失关键字 SQL> select sal 调前,case job when 'PRESIDENT' then sal + 1000 when 'MANAGER' then sal + 800 sal + 400 end 涨后 from emp;
select sal 调前,case job when 'PRESIDENT' then sal + 1000 when 'MANAGER' then sal + 800 sal + 400 end 涨后 from emp
*
第 1 行出现错误:
ORA-00905: 缺失关键字 SQL>
SQL> select sal 调前,case when job = 'PRESIDENT' then sal + 1000 when job = 'MANAGER' then sal + 800 sal + 400 end 涨后 from emp;
select sal 调前,case when job = 'PRESIDENT' then sal + 1000 when job = 'MANAGER' then sal + 800 sal + 400 end 涨后 from emp
*
第 1 行出现错误:
ORA-00905: 缺失关键字 SQL> select sal, case
2 when job = 'MANAGER' then sal + 800
3 end from emp; SAL CASEWHENJOB='MANAGER'THENSAL+800END
---------- -----------------------------------
800
1600
1250
2975 3775
1250
2850 3650
2450 3250
3000
5000
1500
1100 SAL CASEWHENJOB='MANAGER'THENSAL+800END
---------- -----------------------------------
950
3000
1300 已选择14行。 SQL> spool off;

  

Oracle Day2 过滤、排序、单行函数的更多相关文章

  1. Oracle学习笔记_03_单行函数

    1.SQL函数: 单行函数.多行函数 单行函数:       操作数据对象       接受参数返回一个结果       只对一行进行变换       每行返回一个结果       可以转换数据类型  ...

  2. Oracle学习第二篇—单行函数

    1字符函数 length  字符长度 lengthb 字节长度 lower 变为小写 upper 变为大写 initcap 首字母大写 select Lower('xun Ying') 小写,Uppe ...

  3. oracle 10g 学习之单行函数(5)

    目标 通过本章学习,您将可以: l  SQL中不同类型的函数. l  在 SELECT 语句中使用字符,数字和日期函数. l  描述转换型函数的用途. 字符函数 字符函数分为大小写控制函数和字符控制函 ...

  4. oracle强化练习之单行函数

    1.      显示dname和loc中间用-分隔 Select dname ||'-'|| loc From dept; 2.      将部门名称左填充为10位 Select lpad( dnam ...

  5. Oracle01——基本查询、过滤和排序、单行函数、多行函数和多表查询

    作者: kent鹏 转载请注明出处: http://www.cnblogs.com/xieyupeng/p/7272236.html Oracle的集群 Oracle的体系结构 SQL> --当 ...

  6. Oracle学习总结_day03_day04_条件查询_排序_函数_子查询

    本文为博主辛苦总结,希望自己以后返回来看的时候理解更深刻,也希望可以起到帮助初学者的作用. 转载请注明 出自 : luogg的博客园 谢谢配合! day03_条件查询_排序_函数 清空回收站: PUR ...

  7. Netsuite Formula > Oracle函数列表速查(PL/SQL单行函数和组函数详解).txt

    PL/SQL单行函数和组函数详解 函数是一种有零个或多个参数并且有一个返回值的程序.在SQL中Oracle内建了一系列函数,这些函数都可被称为SQL或PL/SQL语句,函数主要分为两大类: 单行函数 ...

  8. Oracle笔记(五) 单行函数

    虽然各个数据库都是支持SQL语句的,但是每一个数据库也有每一个数据库自己所支持的操作函数,这些就是单行函数,而如果要想进行数据库开发的话,除了要会使用SQL之外 ,就是要多学习函数. 单行函数主要分为 ...

  9. Oracle 单行函数

    一.什么是函数 任何东西,只要它能接收输入,对输入进行加工并产生输出,它就可以被称为函数. 二.单行函数简介 单行函数只对表中的一行数据进行操作,并且对每一行数据只产生一个输出结果.单行函数可以接受一 ...

随机推荐

  1. Python学习笔记——基础篇【第五周】——算法(4*4的2维数组和冒泡排序)、时间复杂度

    目录 1.算法基础 2.冒泡排序 3.时间复杂度 (1)时间频度 (2)时间复杂度 4.指数时间 5.常数时间 6.对数时间 7.线性时间 1.算法基础  要求:生成一个4*4的2维数组并将其顺时针旋 ...

  2. HTML5 <meta> 标签属性

    基本标签SEO 优化为移动设备添加 viewportWindows 8其他 禁止数字识自动别为电话号码不让android识别邮箱每 8 秒刷新一次页面移动端的头部标签和meta 基本标签 声明文档使用 ...

  3. Zabbix 监控rabbitmq

    一 应用场景描述 测试环境业务 OpenStack中搭建了rabbitmq作为RPC消息队列,所以mq起到了很重要的角色,本案例是来如何使用zabbix监控 rabbitmq业务 二 Rabbitmq ...

  4. vim menu乱码

    syntax enable syntax on colorscheme desert set nocompatible set filetype=c set number set wrap " ...

  5. python学习day2

    一.模块初识 python模块 模块让你能够有逻辑地组织你的Python代码段. 把相关的代码分配到一个 模块里能让你的代码更好用,更易懂. 模块也是Python对象,具有随机的名字属性用来绑定或引用 ...

  6. SNS

    SNS,专指社交网络服务.也指社交现有已成熟普及的信息载体,如短信SMS服务.SNS的另一种常用解释:全称Social Network Site,即“社交网站”或“社交网”.SNS也指Social N ...

  7. javaWEB总结(13):域对象的属性操作

    前言 本文主要讲解javaweb的四个域对象以及他们的作用范围,后面会有小demo来具体测试. 四个域对象 (1)pageContext:属性的作用范围仅限于当前JSP页面: (2)request:属 ...

  8. iis虚拟目录或应用程序不继承父站点的web.config配置信息

    A为主站点 B为A的应用程序站点 再A的web.config中对不想继承的节点用location 套起来.如下: <location path="." allowOverri ...

  9. 【第一篇】Volley的使用之json请求

    最近项目写完,有开始新的学习了,volley很久以前就接触了,也看了源码,然而却没有通过文章去记录自己的学习成果. 首先讲下volley的特点: 1,扩展性强.Volley 中大多是基于接口的设计,可 ...

  10. C# cookies

    谷歌浏览器- 工具---internet选项---常规---浏览历史记录---设置  cookie和网站数据 C:\Documents and Settings\Administrator\Local ...