Oracle基本语句整理
- 在sql*plus中修改当前会话的日期格式
Alter session set nls_date_format = ‘yyyy-mm-dd hh24:mi:ss’;
ALTER SESSION SET NLS_NLS_DATE-FORMAT
- 根据时间范围查询语句
Select * from emp where hiredate between to_date(‘19810401’)and to_date(‘19810501’)
- 根据排序查询固定语句
Select * from emp where rownum<=10 order by sal asc(desc)
- 表关联查询
Select dept.dname,emp.ename from emp,dept where emp.deptno=dept.deptno
- 查询数据库中从n到m条数据
(select * from emp where rownum<=10) minus (select * from emp where rownum <=20)
说明 :minus 关键字的意思是求两个结果集的差集,在数学中有这个概念,比如说两个集合可以合并、公有、差集.
Select * from (select * from emp where rownum<=20) b where b.empno not in(select empno from emp where rownum <10)
主要运用not in 运算
- 对字符串的操作(dual是一个虚拟表,用来构成select的语法规则,oracle保证dual里面永远只有一条记录)
- 字符串连接
select 'abc' || 'def' from dual;
->'ABC'|
------
abcdef
- 小写
select lower('ABC012');
->lower
--------
abc012
大写
select upper('abc012');
->upper
--------
ABC012
- 左补全
select lpad('abc', 5, '0');
->lpad
-------
00abc
右补全
select rpad('abc', 5, '0');
->rpad
-------
abc01
- 左空白删除
select ltrim(' abc');
->ltrim
-------
abc
右空白删除
select ltrim('abc ');
-> ltrim
---------------
Abc
左右空白删除
select trim(' abc ');
->btrim
-------
abc
- 字符串替换
Select translate(‘ababab’,’a’,’1’)from dual;
Transl
-------
1b1b1b
- 取子字符串
select substr('abc012', 3, 2) from dual;
->SU
---------
c0
- 字符串长度
select length('abc012') from dual;
->length('ABC012')
----------------
6
- 数字转化成字符串
select to_char(123456, '999,999,999,999') from dual;
->TO_CHAR(123456,'
-----------
123456
- 字符串转化为数字
select to_number('123' || '456', '999999999999') from dual;
->To_number('123'||'456','999999999999')
-------------
123456
select to_number('123,456', '999,999,999,999') from dual;
->To_number('123,456','999999999999')
-------------
123456