–字符函数
select upper(‘Hello world ‘) as 转大写, lower(‘Hello world’) 转小写,initcap (‘Hello world’)首字母大写 from dual;
运行结果:
转大写 转小写 首字母大写
----------- ----------- -----------
HELLO WORLD hello world Hello World
–substr(a,b) 从 a中,第b位开始包含b,下标从1开始,取右边所有的字符
select substr(‘Hello World’,3) from dual;
SUBSTR('H
-------------------
llo World
–substr(a,b,c) 从a中,第b位开始,取c位
select substr(‘Hello World’,3,4) from dual;
SUBS
>----
llo
–length 字符数 lengthb 字节数
select length(‘Hell World’) 字符,lengthb(‘Hell World’) 字节
2 from dual;
字符 字节
---------- ----------
10 10
select length(‘中国’) 字符,lengthb(‘中国’) 字节* from dual
字符 字节
---------- ----------
2 4
–instr(a,b) 在a中,查找b,找到返回下标;否则返回0
select instr(‘Hello World’,’ll’)as instr from dual;
INSTR
------------------------
3
–lpad 左填充 rpad 右填充
– abcd –> 10位
select lpad(‘abcd’,10,’‘) 左,rpad(‘abcd’,10,’‘) 右 from dual;
左 右
---------- ----------
******abcd abcd******
–replace 替换
select replace(‘Hello World’,’l’,’*’) from dual;
REPLACE('HE
-----------
He**o Wor*d
Case表达式,是可以在sql中使用if ..then..else的逻辑判断,而避免使用PL/SQL的有效方法
select ename,job,sal as 涨前,
case job when ‘SALESMAN’ then sal+100
when ‘CLERK’ then sal+800
when ‘MANAGER’ then sal+500
else sal+400
end as 涨后 from emp;
多行函数
–查询工资总和
select sum(sal) from emp;
SUM(SAL)
----------
29025
–emp表中的总人数
select count(*) from emp;
COUNT(*)
----------
14
–平均工资
select avg(sal) from emp;
AVG(SAL)
----------
2200
GROUP BY 语句用于组合函数, sum,avg,count等为组合函数,根据一个或多个列对结果集进行分组
—求部门(-deptno)的平均工资(-avg(sal)). (按照部分分组。)
select deptno,avg(sal) from emp group by deptno;
DEPTNO AVG(SAL)
---------- ----------
30 1566.66667
20 2175
10 2916.66667
where 子句的作用是在对查询结果进行分组前,将不符合where条件的行去掉,即在分组之前过滤数据,条件中不能包含聚组函数,使用where条件显示特定的行。having 子句的作用是筛选满足条件的组,即在分组之后过滤数据,条件中经常包含聚组函数,使用having 条件显示特定的组,也可以使用多个分组标准进行分组。having 子句被限制子已经在SELECT语句中定义的列和聚合表达式上。通常,你需要通过在HAVING子句中重复聚合函数表达式来引用聚合值,就如你在SELECT语句中做的那样
–查询平均工资–(avg(sal))大于2000–(>2000)的部门–(deptno)。
select deptno, avg(sal) from emp group by deptno having avg(sal)>2000;
where 和having 的最大的区别: where后面不能使用组函数。