Description

The Oracle/PLSQL NVL function lets you substitute a value when a null value is encountered.

NVL函数是当出现空值时替换一个值

Syntax

NVL( string1, replace_with )

String1

The string to test for a null value.

replace_with

The value returned if string1 is null.

Example

For example:

SELECT NVL(supplier_city, 'n/a')
FROM suppliers;

The SQL statement above would return 'n/a' if the supplier_city field contained a null value. Otherwise, it would return the supplier_city value.

SELECT supplier_id,
NVL(supplier_desc, supplier_name)
FROM suppliers;

This SQL statement would return the supplier_name field if the supplier_desc contained a null value. Otherwise, it would return the supplier_desc.

SELECT NVL(commission, 0)
FROM sales;

This SQL statement would return 0 if the commission field contained a null value. Otherwise, it would return the commission field.

Frequently Asked Questions

Question:

Answer:

I tried to use the NVL function through VB to access Oracle DB.

To be precise,

SELECT NVL(Distinct (emp_name),'AAA'),................
FROM.................

I got an oracle error when I use distinct clause with NVL, but when I remove distinct it works fine.

It is possible to the use the DISTINCT clause with the NVL function. However, the DISTINCT must come before the use of the NVL function. For example:

SELECT distinct NVL(emp_name, 'AAA')
FROM employees;

Is it possible to use the NVL function with more than one column with the same function call? To be clear, if i need to apply this NVL function to more than one column like this:

NVL(column1;column2 ...... , here is the default value for all )

Answer: You will need to make separate NVL function calls for each column. For example:

SELECT NVL(table_name, 'not found'), NVL(owner, 'not found')
FROM all_tables;

Examine the TRAINING table as given below:

Name                                      Null?    Type
 ----------------------------------------- -------- -------------

TRAINING_ID                               NOT NULL NUMBER(5)
 TRAINING_LOCATION                                  NUMBER(7,2)
 START_DATE                                         DATE
 END_DATE                                           DATE

Which two SQL would execute successfully? (Choose two)

  1. SELECT NVL (ADD_MONTHS (END_DATE,1),SYSDATE) FROM training;
  1. SELECT TO_DATE (NVL(SYSDATE-END_DATE,SYSDATE)) FROM training;
  2. SELECT NVL(MONTHS_BETWEEN(START_DATE,END_DATE),’In Progress’) FROM training;
  1. SELECT NVL(TO_CHAR(MONTHS_BETWEEN(START_DATE,END_DATE)),’In Progress’) FROM training;

A, D. Use NVL function to provide an alternate value to a column when NULL.

使用DISTINCT 的方法COUNT函数和NVL函数的区别:

NVL

SELECT DISTINCT NVL(emp_name, 'AAA')
FROM employees;

COUNT

SELECT COUNT(DISTINCT department) AS "Unique departments"
FROM employees
WHERE salary > 55000;

From <https://www.techonthenet.com/oracle/functions/nvl.php>

Oracle Function: NVL的更多相关文章

  1. Oracle Function:COUNT

    Description The Oracle/PLSQL COUNT function returns the count of an expression. The COUNT(*) functio ...

  2. Oracle Function:TO_CHAR

    Description The Oracle/PLSQL TO_CHAR function converts a number or date to a string.将数字转换为日期或字符串 Syn ...

  3. oracle中空值null的判断和转换:NVL的用法

    1.NULL空值概念 数据库里有一个很重要的概念:空值即NULL.有时表中,更确切的说是某些字段值,可能会出现空值, 这是因为这个数据不知道是什么值或根本就不存在. 2.NULL空值判断 空值不等同于 ...

  4. 问题:oracle nvl;结果:Oracle中的NVL函数

    Oracle中的NVL函数 (2012-11-30 13:21:43) 转载▼ 标签: nvl oracle 分类: Oracle Oracle中函数以前介绍的字符串处理,日期函数,数学函数,以及转换 ...

  5. oracle function学习1

    oracle function学习基层: 函数就是一个有返回值的过程.  首先 知道oracle 使用限制:      函数调用限制: 1. SQL语句中只能调用存储函数(服务器端),而不能调用客户端 ...

  6. Oracle中的NVL函数

    Oracle中函数以前介绍的字符串处理,日期函数,数学函数,以及转换函数等等,还有一类函数是通用函数.主要有:NVL,NVL2,NULLIF,COALESCE,这几个函数用在各个类型上都可以. 下面简 ...

  7. oracle中的nvl(), nvl2()函数

    nvl()函数是oracle/plpgsql中的一个函数,格式为:nvl(string1, replace_with) 功能:如果string1 位null,那么nvl()函数返回replace_wi ...

  8. Oracle中的NVL,NVL2,NULLIF以及COALESCE函数使用

    首先注意空(null)值,空值加任何值都是空值,空值乘任何值也都是空值,依此类推. 1.NVL函数 NVL函数的格式如下:NVL(expr1,expr2) 含义是:如果oracle第一个参数为空那么显 ...

  9. [转载]Oracle中的NVL函数

    Oracle中函数以前介绍的字符串处理,日期函数,数学函数,以及转换函数等等,还有一类函数是通用函数.主要有:NVL,NVL2,NULLIF,COALESCE,这几个函数用在各个类型上都可以. 下面简 ...

随机推荐

  1. Objective-C 协议和运行时检查方法、类是否存在

    协议的声明: // // Person.h // TestOC01 // // Created by xinye on 13-10-23. // Copyright (c) 2013年 xinye. ...

  2. 时间戳Id

    ID:格式据1970.1.1毫秒数 1535091029740  13位 问题:高并发,分布式明显会有问题,网上有雪花算法,但是位数跟我需要的不一样,暂时不考虑,以后研究再用. 参考: https:/ ...

  3. Python学习--字符串slicing

    Found this great table at http://wiki.python.org/moin/MovingToPythonFromOtherLanguages Python indexe ...

  4. Linux命令之乐--sed

    sed是stream edit的缩写,是处理文本非常重要的工具. 常见用法: 1. 替换文本 1.1 替换文本中的第一处符合的样式 sed 's/pattern/replace_string' fil ...

  5. react native (一)

    开始接触app方面的工作,真心塞~ 又开始了周而复始的死磕一个问题专坐一整天的节奏,关键是还没有成绩,实在无语.╮(╯▽╰)╭,还是总结一下最近心塞历程吧-- react native中文网:http ...

  6. 简析iOS动画原理及实现——Core Animation

    本文转载至 http://www.tuicool.com/articles/e2qaYjA 原文  https://tech.imdada.cn/2016/06/21/ios-core-animati ...

  7. linux CentOS 7 安装 RabbitMQ 3.6.3, Erlang 19.0

    1. 安装erlang 安装依赖环境 yum -y install make gcc gcc-c++ kernel-devel m4 ncurses-devel openssl-devel unixO ...

  8. python框架---->pymysql的使用

    这里面学习一下python中操作mysql的第三方库pymysql的使用.很多我们以为一辈子都不会忘掉的事情,就在我们念念不忘的日子里.被我们遗忘了. pymysql的简单使用 我们创建一张表来进行下 ...

  9. 链表一元多项式计算器的实现(Java语言描述)

    链表的经典应用,程序在多项式相加同类项合并还有小的瑕疵,有待改进. 代码如下: package 一元多项式计算器; public class PolyNode { private double a; ...

  10. Android 屏幕适配:最全面的解决方案

    转自:https://www.jianshu.com/p/ec5a1a30694b 前言 Android的屏幕适配一直以来都在折磨着我们Android开发者,本文将结合: Google的官方权威适配文 ...