• Oracle Function: NVL


    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>

  • 相关阅读:
    语法上的小trick
    [算法模版]斜率优化
    CF886E Maximum Element
    【hdu 1576】A/B(数论--拓展欧几里德 求逆元 模版题)
    【poj 3090】Visible Lattice Points(数论--欧拉函数 找规律求前缀和)
    【poj 2478】Farey Sequence(数论--欧拉函数 找规律求前缀和)
    【poj 1284】Primitive Roots(数论--欧拉函数 求原根个数){费马小定理、欧拉定理}
    【poj 2407】Relatives(数论--欧拉函数 模版题)
    【bzoj 2038】 [2009国家集训队]小Z的袜子(算法效率--莫队分块算法 模版题)
    【uva 1312】Cricket Field(算法效率--技巧枚举)
  • 原文地址:https://www.cnblogs.com/thescentedpath/p/NLV.html
Copyright © 2020-2023  润新知