Каталог статей
Меню сайта


Форма входа


Категории раздела
Oracle DB administering [46]
Oracle DB programming [15]
Oracle DB troubleshooting [11]


Поиск


Друзья сайта
  • Официальный блог
  • Сообщество uCoz
  • FAQ по системе
  • Инструкции для uCoz


  • Статистика

    Онлайн всего: 1
    Гостей: 1
    Пользователей: 0


    Приветствую Вас, Гость · RSS 19.05.2024, 19:53
    Главная » Статьи » Oracle DB » Oracle DB programming

    NVL and COALESCE

    http://stackoverflow.com/questions/950084/oracle-differences-between-nvl-and-coalesce

    NVL and COALESCE are used to achieve the same functionality of providing a default value in case the column returns a NULL.

    The differences are:

    1. NVL accepts only 2 arguments whereas COALESCE can take multiple arguments
    2. NVL evaluates both the arguments and COALESCE stops at first occurrence of a non-Null value.
    3. NVL does a implicit datatype conversion based on the first argument given to it. COALESCE expects all arguments to be of same datatype.
    4. COALESCE gives issues in queries which use UNION clauses. Example below
    5. COALESCE is ANSI standard where as NVL is Oracle specific.

    Examples for the third case. Other cases are simple.

    select nvl('abc',10) from dual; would work as NVL will do an implicit conversion of numeric 10 to string.

    select coalesce('abc',10) from dual; will fail with Error - inconsistent datatypes: expected CHAR got NUMBER

    Example for UNION use-case

    SELECT COALESCE(a, sysdate) 
    from (select null as a from dual 
     union 
     select null as a from dual
     );

    fails with ORA-00932: inconsistent datatypes: expected CHAR got DATE

    SELECT NVL(a, sysdate) 
    from (select null as a from dual 
     union 
     select null as a from dual
     ) ;

    succeeds.

    Категория: Oracle DB programming | Добавил: basil (14.03.2016)
    Просмотров: 380 | Рейтинг: 0.0/0
    Всего комментариев: 0
    Имя *:
    Email *:
    Код *:
    Бесплатный конструктор сайтов - uCoz