oracle 数据库学习3 --oracle 数据库中常使用的函数

1.字符函数:

ASCII(X) -->返回X字符的ASCII码

concat(x,y)-->将y连接到X上输出

select concat(first_name,last_name) from customers;

INITCAP(x) 将x中每个单词的首字母大写

INSTR('namename','e',1,2)  -->从'namename'开始的第一个字母开始找'e'第二次出现的位置;

LENGTH(x) 获取X字符串的长度;

LOWER(x) 将x中的字母转换为小写;

UPPER(x) 将X中的字母转换为大写;

LPAD(x,10,'n')-->在X字符串的左边补'n'字符,使x的总长度达到10个字符;

RPAD(x,10,'r')

LTRIM

例如:

LTRIM('    hello  ,my friends! ') --> 默认截取左边的空字符串

RTRIM('HI this is a dog.','dog.')-->截取右边的'dog.'字符

NVL  nvl(phone,'this is a null value')--将phone字段中空值转换为字符串'this is a null value'

NVL2(x,value1,value2) 如果X为非空,则为value1,否则返回value2

REPLACE('name','m','n')  ->将name中的'm'替换为n

SUBSTR(name,2,7) 从name的第二个字符开始,取长度为7个字符;

2.数字函数

ABS(-10) 得到-10的绝对值10

CELL(5.8) 取得大于或者等于5.8的最小整数 6

CELL(-5.2)  -> -5

FLOOR(5.8) 取得小于或者等于x的最大整数 5

FLOOR(-5.2)  -6

MOD(8,3) ->取8除以5所得的余数 3

POWER(2,1) -》取2的一次方

ROUND(5.75)  对5.75向上取整 为6

to_char(1234.5678,'99999.99') -->将数字转换为字符串,并按照指定的格式

REGEXP_LIKE() -->匹配正则表达式

3.聚合函数

AVG(x) 平均值

COUNT(x)  ,count(*) 比count(1)执行时需要的时间更长,因此避免使用count(*)

MAX(x) 返回X的最大值

MEDIAN(x) 返回X的中间值

MIN(x) 返回X的最小值

STDDEV(x) 返回X的标准差

SUM(x) 返回x的和

GROUP BY 将行分组成具有相同列值的多个部分

聚合函数是和 group by 分组配合使用

例如: select  product_type_id ,AVG(price) from products;  错误 ORA-00937:not a single-group group function.

不能在where子句中用聚合函数

例如: select product_type_id ,avg(price)

from products

where avg(price)>30

group by product_type_id;  错误 ,因为where只能是对单行而不是行组过滤,要过滤分组的行的条件,可以使用having。

正确做法:

select product_type_id ,avg(price)

from products

group by product_type_id

having avg(price)>20;

WHERE 和GROUP BY 的组合使用

 where 对products表中的行进行过滤,只保留price<15的行,然后GROUP BY 对保留的行根据product_type_id进行分组

            select product_type_id ,AVG(price)

             from products

             where price <15

             group by product_type_id

              order by product_type_id;

WHERE 、GROUP BY、HAVING的组合使用

            select product_type_id,AVG(price)

            from products

           where price <15

          group by product_type_id

          having avg(price) >13

          order by price_type_id;

原文地址:https://www.cnblogs.com/daodan/p/6131145.html