it编程 > 数据库 > Oracle

Oracle数据库常用函数总结大全

13人参与 2025-12-08 Oracle

前言

oracle sql 提供了极其丰富的内置函数库,这些函数是数据处理、查询和分析的强大武器。本教程将系统地介绍各类常用函数,并为每个函数提供独立的示例和注释结果。

思维导图

一、字符函数

1.1 upper(string)

select upper('hello oracle') from dual;
-- 返回: 'hello oracle'

1.2 lower(string)

select lower('hello oracle') from dual;
-- 返回: 'hello oracle'

1.3 initcap(string)

select initcap('hello oracle world') from dual;
-- 返回: 'hello oracle world'

1.4 length(string)

select length('oracle sql') from dual;
-- 返回: 10

1.5 instr(string, substring, [start_position], [nth_appearance])

select instr('oracle sql is cool sql', 'sql', 1, 2) from dual;
-- 返回: 21 (从第1个字符开始查找,第2次出现的'sql'的位置)

1.6 substr(string, start_position, [length])

select substr('oracle database', 8, 8) from dual;
-- 返回: 'database' (从第8个字符开始,截取8个字符)

1.7 replace(string, search_string, [replacement_string])

select replace('black cat and blue cat', 'cat', 'dog') from dual;
-- 返回: 'black dog and blue dog'

1.8 concat(string1, string2)

select concat('hello', ' world') from dual;
-- 返回: 'hello world'
select 'oracle' || ' ' || 'sql' from dual;
-- 返回: 'oracle sql'

1.9 lpad(string, length, [pad_string])

select lpad('123', 5, '0') from dual;
-- 返回: '00123'

1.10 rpad(string, length, [pad_string])

select rpad('abc', 5, '*') from dual;
-- 返回: 'abc**'

1.11 trim(string)

select trim('  oracle  ') from dual;
-- 返回: 'oracle'

1.12 ltrim(string, [set])

select ltrim('$$$100', '$') from dual;
-- 返回: '100'

1.13 rtrim(string, [set])

select rtrim('abc##', '#') from dual;
-- 返回: 'abc'

二、数值函数

2.1 round(number, [decimal_places])

select round(123.456, 2) from dual;
-- 返回: 123.46

2.2 trunc(number, [decimal_places])

select trunc(123.456, 2) from dual;
-- 返回: 123.45

2.3 ceil(number)

select ceil(99.1) from dual;
-- 返回: 100

2.4 floor(number)

select floor(99.9) from dual;
-- 返回: 99

2.5 mod(m, n)

select mod(10, 3) from dual;
-- 返回: 1

2.6 abs(number)

select abs(-123) from dual;
-- 返回: 123

三、日期函数

3.1 sysdate

select sysdate from dual;
-- 返回: (当前日期和时间,例如 2024-03-22 10:30:00)

3.2 systimestamp

select systimestamp from dual;
-- 返回: (当前日期时间+小数秒+时区,例如 22-mar-24 10.30.00.123456 am +08:00)

3.3 add_months(date, integer)

select add_months(to_date('2024-01-31', 'yyyy-mm-dd'), 1) from dual;
-- 返回: 29-feb-24 (会自动处理月末日期)

3.4 months_between(date1, date2)

select months_between(to_date('2024-07-15', 'yyyy-mm-dd'), to_date('2024-01-15', 'yyyy-mm-dd')) from dual;
-- 返回: 6

3.5 last_day(date)

select last_day(to_date('2024-02-10', 'yyyy-mm-dd')) from dual;
-- 返回: 29-feb-24 (2024是闰年)

3.6 next_day(date, ‘day_of_week’)

select next_day(to_date('2024-03-22', 'yyyy-mm-dd'), '星期一') from dual; -- 假设nls_date_language是中文
-- 返回: 25-mar-24

3.7 trunc(date, [format_model])

select trunc(sysdate, 'mm') from dual;
-- 返回: (当月的第一天,例如 01-mar-24)

3.8 extract(unit from date)

select extract(year from sysdate) from dual;
-- 返回: (当前年份,例如 2024)

四、转换函数

4.1 to_char(date/number, [format_model])

select to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss') from dual;
-- 返回: '2024-03-22 10:30:00' (示例)
select to_char(12345.67, 'fm99g999d00') from dual;
-- 返回: '12,345.67'

4.2 to_date(string, [format_model])

select to_date('2024/01/15', 'yyyy/mm/dd') from dual;
-- 返回: 15-jan-24 (日期类型)

4.3 to_number(string, [format_model])

select to_number('1,234.56', '9,999.99') from dual;
-- 返回: 1234.56 (数字类型)

五、聚合函数

(通常与 group by 配合使用,此处为简化,对全表操作)

5.1 count(*) / count(column) / count(distinct column)

-- 假设 employees 表有10条记录, 其中 commission_pct 有3个非空值,2种不同的非空值
select count(*), count(commission_pct), count(distinct commission_pct) from employees;
-- 返回: 10, 3, 2

5.2 sum(expression)

select sum(salary) from employees;
-- 返回: (所有员工薪水总和)

5.3 avg(expression)

select avg(salary) from employees;
-- 返回: (所有员工薪水平均值)

5.4 max(expression)

select max(salary) from employees;
-- 返回: (最高薪水)

5.5 min(expression)

select min(salary) from employees;
-- 返回: (最低薪水)

六、通用/其他函数

6.1 nvl(expr1, expr2)

select nvl(commission_pct, 0) from employees;
-- 返回: (如果commission_pct是null,则显示0,否则显示其本身的值)

6.2 nvl2(expr1, expr2, expr3)

select nvl2(commission_pct, 'has commission', 'no commission') from employees;
-- 返回: (根据commission_pct是否为null,显示不同的字符串)

6.3 decode(expr, search1, result1, … [default])

select department, decode(department, 'sales', 's', 'hr', 'h', 'other') as dept_code from employees;
-- 返回: (将部门名转换为代码)

6.4 case when … end

select salary, case when salary > 10000 then 'high' else 'normal' end as salary_level from employees;
-- 返回: (根据薪水是否大于10000,显示不同的等级)

练习题

背景表:employees

create table employees (
    employee_id     number(10) not null,
    full_name       varchar2(100 char) not null,
    job_title       varchar2(100 char) not null,
    department      varchar2(50 char) not null,
    salary          number(10, 2) not null,
    commission_pct  number(4, 2),
    hire_date       date not null,
    constraint employees_pk primary key (employee_id)
);

题目:

  1. 查询所有员工的全名,格式为 “名 姓” (例如, ‘john doe’),并且所有字母都为大写。
  2. 查询所有员工入职至今的完整月数,结果四舍五入到整数。
  3. 查询所有员工的姓氏 (即 full_name 中逗号前的部分)。
  4. 查询所有员工的薪资等级。如果薪水大于10000,等级为’a’;如果在5000到10000之间 (含),等级为’b’;否则为’c’。
  5. 计算每个部门的员工总数和平均薪资。
  6. 查询所有员工的总收入。总收入 = salary + (salary * commission_pct)。注意 commission_pct 可能为null,如果为null,则提成视为0。
  7. 查询所有员工的入职日期,格式为 “yyyy年mm月dd日”。
  8. 查询每个员工以及其所在部门中薪水次高的员工的薪水。如果该员工已经是薪水最高的,则显示null。
  9. 查询所有员工的姓氏,并确保首字母大写,其余小写,同时去除可能存在的前后空格。
  10. 查询每个员工入职当月的最后一天是星期几 (英文全称)。

答案与解析:

  1. 查询并格式化全名:
select upper(substr(full_name, instr(full_name, ',') + 2) || ' ' || substr(full_name, 1, instr(full_name, ',') - 1)) as formatted_name
from employees;
  1. 计算入职月数:
select full_name, round(months_between(sysdate, hire_date)) as months_worked
from employees;
  1. 提取姓氏:
select substr(full_name, 1, instr(full_name, ',') - 1) as last_name
from employees;
  1. 划分薪资等级:
select full_name, salary,
       case
         when salary > 10000 then 'a'
         when salary between 5000 and 10000 then 'b'
         else 'c'
       end as salary_grade
from employees;
  1. 按部门聚合计算:
select department, count(*) as number_of_employees, round(avg(salary), 2) as average_salary
from employees
group by department;
  1. 计算总收入 (处理null):
select full_name, salary + (salary * nvl(commission_pct, 0)) as total_income
from employees;
  1. 格式化入职日期:
select full_name, to_char(hire_date, 'yyyy"年"mm"月"dd"日"') as formatted_hire_date
from employees;
  1. 查询部门次高薪水 (lag):
select full_name, department, salary,
       lag(salary, 1, null) over (partition by department order by salary desc) as next_highest_salary
from employees;
  1. 格式化姓氏:
select initcap(trim(substr(full_name, 1, instr(full_name, ',') - 1))) as cleaned_last_name
from employees;
  1. 入职月最后一天是星期几:
select full_name, hire_date, to_char(last_day(hire_date), 'day') as last_day_of_hire_month
from employees;

总结

到此这篇关于oracle数据库常用函数的文章就介绍到这了,更多相关oracle常用函数内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

您想发表意见!!点此发布评论

推荐阅读

Oracle ADG相关查询的实现

12-10

Oracle杀会话的实现步骤

12-10

Oracle数据库实现查询所有表

12-02

Nginx概念、架构、配置与虚拟主机实战操作指南

11-26

Oracle数据库物理备份与恢复实战教程

11-26

ORACLE RMAN复制数据库(Duplicate)详细指南

11-25

猜你喜欢

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论