Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

General Functions: Nvl, Nvl2, Decode, Coalesce, Nullif, Lnnvl And Nanvl in SQL

General Functions: Nvl, Nvl2, Decode, Coalesce, Nullif, Lnnvl And Nanvl in SQL

Sure! Here's a clear explanation of those general functions related to handling NULLs and conditional logic in SQL:


General Functions: NVL, NVL2, DECODE, COALESCE, NULLIF, LNNVL, NANVL


1. NVL(expr1, expr2)

  • Replaces NULL value expr1 with expr2.

  • Returns expr1 if it is not NULL; otherwise, returns expr2.

SELECT NVL(NULL, 'Default') FROM dual;   -- Returns 'Default'SELECT NVL('Hello', 'Default') FROM dual; -- Returns 'Hello'

Common in Oracle.


2. NVL2(expr1, expr2, expr3)

  • If expr1 is not NULL, returns expr2;

  • Otherwise, returns expr3.

SELECT NVL2('Test', 'Not Null', 'Is Null') FROM dual;  -- Returns 'Not Null'SELECT NVL2(NULL, 'Not Null', 'Is Null') FROM dual;    -- Returns 'Is Null'

3. DECODE(expr, search, result [, search, result ...] [, default])

  • Compares expr to each search value; returns corresponding result if matched; otherwise, returns default (or NULL if default is not given).

  • Works like a simple IF-THEN-ELSE or CASE statement.

SELECT DECODE(2, 1, 'One', 2, 'Two', 'Other') FROM dual;  -- Returns 'Two'SELECT DECODE(3, 1, 'One', 2, 'Two', 'Other') FROM dual;  -- Returns 'Other'

4. COALESCE(expr1, expr2, ..., exprN)

  • Returns the first non-NULL expression in the list.

  • More flexible than NVL because it can take multiple arguments.

SELECT COALESCE(NULL, NULL, 'Third', 'Fourth') FROM dual;  -- Returns 'Third'

5. NULLIF(expr1, expr2)

  • Returns NULL if expr1 = expr2; otherwise, returns expr1.

  • Useful to avoid division by zero or similar conditions.

SELECT NULLIF(10, 10) FROM dual;  -- Returns NULLSELECT NULLIF(10, 5) FROM dual;   -- Returns 10

6. LNNVL(condition)

  • Logical Negation of Null Value Logic.

  • Returns TRUE if condition is FALSE or UNKNOWN (NULL).

  • Returns FALSE if condition is TRUE.

  • Mainly used in WHERE clauses to handle three-valued logic with NULLs.

SELECT * FROM employees WHERE LNNVL(salary > 5000);-- Returns rows where salary <= 5000 or salary IS NULL

7. NANVL(expr1, expr2)

  • Returns expr2 if expr1 is NaN (Not a Number); otherwise returns expr1.

  • Used to handle NaN values in floating-point operations.

SELECT NANVL(0/0, 0) FROM dual;  -- Returns 0 (since 0/0 is NaN)

Summary Table:

FunctionPurposeReturns
NVLReplace NULL with a valueexpr2 if expr1 is NULL else expr1
NVL2Conditional on NULLexpr2 if expr1 NOT NULL, else expr3
DECODESimple if-then-else (like CASE)Corresponding result based on matching expr
COALESCEFirst non-NULL valueFirst non-NULL expression
NULLIFReturn NULL if two expressions equalNULL if equal, else first expression
LNNVLLogical negation with NULL logicTRUE if condition FALSE or NULL, else FALSE
NANVLHandle NaN valuesSecond expr if first is NaN, else first expr

If you want examples with your data or specific SQL dialect (Oracle, PostgreSQL, etc.), just ask!

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql