Usage of ISNULL,NULLIF,COALESCE in SQL

Source: Internet
Author: User

IsNull is to determine if NULL

And Nullif is to change the value to null.

Coalesce is replacing null with something else.

SELECT Employee_id,first_name,last_name,nullif (sales_quota,-1) as QUOTA

From Employees

is to turn 1 into NULL.

COALESCE (expression 1, expression 2,.... Expression N)

From the front to the back, who is not NULL to show who

Select Employee_id,first_name,last_name,

COALESCE (Appt_quota, (Select Min (Appt_quota) from employees), 0) as quota
From Employees
Where Department = ' Marketing '

Report:
ISNULL (Check_expression, Replacement_value)

    • Check_expression and Replacement_value data types must be consistent
    • If Check_expression is NULL, Replacement_value is returned
    • If Check_expression is not NULL, the check_expression is returned

Nullif is used to check two expressions, syntax:
Nullif (expression, expression)

      • Returns null if two expression is equal, which is the data type of the first expression
      • If two expression is not equal, the first expression is returned

Example 1:

CREATE TABLE Test_table (
Idnum number,
CAT VARCHAR2 (200),
Price number
);
INSERT into Test_table VALUES (1, ', 10);
INSERT into Test_table VALUES (2, ' cat1 ', 10);
INSERT into Test_table VALUES (3, ' CAT1 ', 3);
INSERT into Test_table VALUES (4, ' Cat2 ', 11);
INSERT into Test_table VALUES (5, ' cat2 ', 10);
INSERT into Test_table VALUES (6, ', 10);

COALESCE (CAT, ' unknown '),

Result

COALESCE (CAT, ' UNKNOWN ') SUM (price)
Unknown 20
CAT1 13
Cat2 21

Note: Coalesce compared to isnull, coalesce similar case, can be multiple input,isnull only 2 parameter

Coalesce cannot complete conversion of type, otherwise error

Usage of ISNULL,NULLIF,COALESCE in SQL

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.