Oracle Database 12c: SQL Workshop I: 05 Using Conversion Functions and Conditional Expressions

6 篇文章 0 订阅

Using Conversion Functions and Conditional Expressions

Conversion Functions

In some cases, the Oracle server receives data of one data type where it expects data of a different data type. When this happens, the Oracle server can automatically convert the data to the expected data type. This data type conversion can be done implicitly by the Oracle server or explicitly by the user.

Implicit Data Type Conversion

Explicit Data Type Conversion
这里写图片描述

TO_CHAR

SQL> select to_char(sysdate,'fmDdspth "of" Month YYYY fmHH:MI:SS AM') from dual;

TO_CHAR(SYSDATE,'FMDDSPTH"OF"MONTHYYYYFMHH:MI:SSAM')
--------------------------------------------------------------------------------
Thirty-First of August 2018 11:10:27 PM

SQL> select to_char(10002,'L99G999D99') from dual;

TO_CHAR(10002,'L99G999D99')
----------------------------------------
      $10,002.00

这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
TO_NUMBER
TO_DATE

SQL> select to_number('123123.123','999999.000') from dual;

TO_NUMBER('123123.123','999999.000')
------------------------------------
              123123.123

SQL> select TO_DATE('May 24, 1999', 'fxMonth DD, YYYY') from dual;

TO_DATE('MAY24,1999
-------------------
1999-05-24 00:00:00

General Functions
These functions work with any data type and pertain to the use of null values in the expression list.
这里写图片描述
NVL (expr1, expr2)

Syntax
NVL (expr1, expr2)
In the syntax:
• expr1 is the source value or expression that may contain a null
• expr2 is the target value for converting the null
You can use the NVL function with any data type, but the return value is always the same as the data type of expr1
Data types must match:
– NVL(commission_pct,0)
– NVL(hire_date,’01-JAN-97’)
– NVL(job_id,’No Job Yet’)

SQL> desc t4
 Name                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 ID                         NUMBER
 NO                         NUMBER

SQL> select * from t4;

    ID     NO
---------- ----------
     2
     1

SQL> select nvl(no,2) from t4;

 NVL(NO,2)
----------
     2
     2

SQL> select nvl(no,'2') from t4;

NVL(NO,'2')
-----------
      2
      2

SQL> select nvl(no,'a') from t4;
select nvl(no,'a') from t4
              *
ERROR at line 1:
ORA-01722: invalid number

NVL2(expr1, expr2, expr3)

Syntax
NVL2(expr1, expr2, expr3)
In the syntax:
• expr1 is the source value or expression that may contain a null
• expr2 is the value that is returned if expr1 is not null
• expr3 is the value that is returned if expr1 is null
Note: The argument expr1 can have any data type. The arguments expr2 and expr3 can have any data types except LONG.

SQL> select * from t4;

    ID     NO
---------- ----------
     2
     1      1

SQL> select id,nvl2(no,1,0) from t4;

    ID NVL2(NO,1,0)
---------- ------------
     2        0
     1        1

SQL> select id,nvl2(no,1,'a') from t4;
select id,nvl2(no,1,'a') from t4
                    *
ERROR at line 1:
ORA-01722: invalid number

SQL> select id,nvl2(no,'a',0) from t4;

    ID NV
---------- --
     2 0
     1 a

NULLIF (expr1, expr2)

Syntax
NULLIF (expr1, expr2)
In the syntax:
• NULLIF compares expr1 and expr2. If they are equal, the function returns null. If they are not, the function returns expr1. However, you cannot specify the literal NULL for expr1

SQL> select * from t4;

    ID     NO
---------- ----------
     2
            3
     1      1

SQL> select nullif(id,no) from t4;

NULLIF(ID,NO)
-------------
        2



COALESCE (expr1, expr2, … exprn)

Syntax
COALESCE (expr1, expr2, … exprn)
In the syntax:
• expr1 returns this expression if it is not null
• expr2 returns this expression if the first expression is null and this expression is not null
• exprn returns this expression if the preceding expressions are null
Note that all expressions must be of the same data type.

Conditional Expression
CASE Expressions

CASE expr WHEN comparison_expr1 THEN return_expr1
         [WHEN comparison_expr2 THEN return_expr2
          WHEN comparison_exprn THEN return_exprn
          ELSE else_expr]
END

In a simple CASE expression, the Oracle server searches for the first WHEN … THEN pair for which expr is equal to comparison_expr and returns return_expr. If none of the WHEN … THEN pairs meet this condition, and if an ELSE clause exists, the Oracle server returns else_expr. Otherwise, the Oracle server returns a null. You cannot specify the literal NULL for all the return_exprs and the else_expr.
The expressions expr and comparison_expr must be of the same data type, which can be CHAR, VARCHAR2, NCHAR, or NVARCHAR2,NUMBER,BINARY_FLOAT,or BINARY_DOUBLE or must all have a numeric datatype. All of the return values (return_expr) must be of the same data type.
If all expressions have a numeric datatype, then Oracle determines the argument with the highest numeric precedence, implicitly converts the remaining arguments to that datatype, and returns that datatype.

DECODE Functions

DECODE(col|expression, search1, result1
                    [, search2, result2,...,]
                    [, default])

The DECODE function decodes an expression in a way similar to the IF-THEN-ELSE logic that is used in various languages. The DECODE function decodes expression after comparing it to each search value. If the expression is the same as search, result is returned.
If the default value is omitted, a null value is returned where a search value does not match any of the result values.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
前言 1 简介 课程目标1-2 建议日程表1-3 课程目标1-4 Oracle 产品和服务1-5 Oracle Database 10g:“g”代表网格1-6 Oracle 数据库体系结构1-8 数据库结构1-9 Oracle 内存结构1-10 进程结构1-12 Oracle 实例管理1-13 服务器进程和数据库缓冲区高速缓存1-14 物理数据库结构1-15 表空间和数据文件1-17 SYSTEM 和SYSAUX 表空间1-18 段、区和块1-19 逻辑和物理数据库结构1-20 课程示例:HR 方案1-22 数据库体系结构:结构化组件概要1-23 小结1-24 2 安装Oracle 数据库软件 课程目标2-2 Oracle 数据库管理员的任务2-3 用于管理Oracle 数据库的工具2-4 安装:系统要求2-6 检查系统要求2-7 灵活体系结构(OFA) 2-8 使用灵活体系结构2-9 设置环境变量2-11 Oracle Universal Installer (OUI) 2-13 安装Oracle 软件2-14 数据库配置选项2-15 执行配置脚本2-16 完成安装2-17 高级安装选项2-18 安装选项:无提示模式2-19 小结2-20 练习概览:安装Oracle 软件2-21 目录 iii 3 创建Oracle 数据库 课程目标3-2 计划数据库3-3 数据库:示例3-4 Database Configuration Assistant (DBCA) 3-5 使用DBCA 创建数据库3-6 口令管理3-12 创建数据库设计模板3-13 使用DBCA 删除数据库3-14 小结3-16 练习概览:使用DBCA 3-17 4 管理Oracle 实例 课程目标4-2 管理框架4-3 启动和停止Database Control 4-4 Oracle Enterprise Manager 4-5 访问Oracle Enterprise Manager 4-6 数据库主页4-7 使用SQL*Plus 和iSQL*Plus 访问数据库4-8 使用iSQL*Plus 4-9 为了以SYSDBA 和SYSOPER 身份进行访问而设置iSQL*Plus 4-10 使用SQL*Plus 4-12 从Shell 脚本调用SQL*Plus 4-13 从SQL*Plus 调用SQL 脚本4-14 初始化参数文件4-15 简化初始化参数4-16 查看和修改初始化参数4-18 数据库启动和关闭4-19 启动Oracle 数据库实例4-20 启动Oracle 数据库实例:NOMOUNT 4-21 启动Oracle 数据库实例:MOUNT 4-22 启动Oracle 数据库实例:OPEN 4-23 关闭Oracle 数据库实例4-24 关闭模式4-25 SHUTDOWN 选项4-26 使用SQL*Plus 启动和关闭4-29 查看预警日志4-30 查看预警历史记录4-31 动态性能视图4-32 iv 动态性能视图:用法示例4-33 动态性能视图:注意事项4-34 小结4-35 练习概览:管理Oracle 实例4-36 5 管理数据库存储结构 课程目标5-2 存储结构5-3 如何存储表数据5-4 数据库块的结构5-5 表空间和数据文件5-6 Oracle 管理文件(OMF) 5-7 表空间中的空间管理5-8 浏览存储结构5-9 创建新表空间5-10 本地管理表空间的存储5-12 预配置数据库中的表空间5-14 变更表空间5-16 表空间操作5-18 删除表空间5-20 查看表空间信息5-21 采集存储信息5-22 查看表空间内容5-23 扩大数据库5-24 什么是自动存储管理5-25 ASM:主要功能和优点5-26 ASM:概念5-27 小结5-28 练习概览:管理数据库存储结构5-29 6 管理用户安全性 课程目标6-2 数据库用户帐户6-3 预定义帐户:SYS 和SYSTEM 6-5 创建用户6-6 验证用户6-7 管理员验证6-9 解除用户帐户的锁定并重置口令6-10 权限6-11 系统权限6-12 对象权限6-14 v 撤销具有ADMIN OPTION 的系统权限6-15 撤销具有GRANT OPTION 的对象权限6-16 角色的作用6-17 将权限分配给角色以及将角色分配给用户6-18 预定义角色6-19 创建角色6-20 保护角色6-21 将角色分配给用户6-22 概要文件和用户6-23 实施口令安全功能6-25 创建口令概要文件6-27 提供的口令验证函数:VERIFY_FUNCTION 6-28 将限额分配给用户6-29 小结6-31 练习概览:管理用户6-32 7 管理方案对象 课程目标7-2 什么是方案7-3 访问方案对象7-5 命名数据库对象7-6 指定表中的数据类型7-8 创建和修改表7-11 了解数据完整性7-13 定义约束条件7-15 违反约束条件7-16 约束条件状态7-17 约束条件检查7-19 使用SQL 创建约束条件:示例7-20 查看表中的列7-21 查看表的内容7-22 表操作7-23 删除表7-24 截断表7-25 索引7-26 索引类型7-27 B 树索引7-28 位图索引7-30 索引选项7-32 创建索引7-34 vi 什么是视图7-35 创建视图7-36 序列7-37 创建序列7-38
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值