SQL judge null Oracle, SQL server

Source: Internet
Author: User

SQL server

Replace null: isnull (arg, value)

For example, select isnull (price, 0.0) from orders. If the price is null, replace it with 0.0.

Comparison with null: is not null, is null

For example, select * from orders where price is null and price is null.

For example, select * from orders where price is not null, and price is not equal to null

Oracle

Replace null: nvl (arg, value)

For example, select nvl (price, 0.0) form orders

Comparison with null: is not null, is null

For example, select * from orders where price is null and price is null.

For example, select * from orders where price is not null, and price is not equal to null

Oracle

This is a frequently asked question. Especially when the customer used oracle before, he had a question when using SQL server, that is, SQL server and oracle behave differently in processing null values.

In oracle, null values are considered to be infinite values. Therefore, if they are listed in ascending order, they are listed at the end.

In SQL server, the opposite is true. null values are considered to be an infinitely small value. Therefore, if they are sorted in ascending order, they are ranked first.

For example

Select [id]

From [demo]. [dbo]. [orders] order by id

The following results are displayed:

Is there any way to make the default mechanism of SQL server the same as that of oracle? The answer is: no

But we can try some workarounds, such as writing code like below.

Select [id]

From [demo]. [dbo]. [orders] order by case when id is null then 1 else 0 end

In this way, you can see the following results

If this column has an index, you can see the following execution plan

Related Article

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.