Null Value sorting (oracle and sqlserver)

Source: Internet
Author: User
Oracle considers null to be the largest. Sort in ascending order. By default, the null value is placed behind. Sort in descending order. By default, the null value is ranked first. How to change the null value: (1) Use the nvl function or decode function to convert null to a specific value and replace null: nvl (arg, value) (2) use case syntax to convert null to a specific value (later than oracle9i

Oracle considers null to be the largest. Sort in ascending order. By default, the null value is placed behind. Sort in descending order. By default, the null value is ranked first. How to change the null value: (1) Use the nvl function or decode function to convert null to a specific value and replace null: nvl (arg, value) (2) use case syntax to convert null to a specific value (later than oracle9i

Oracle considers null to be the largest.

Sort in ascending order. By default, the null value is placed behind.

Sort in descending order. By default, the null value is ranked first.

How to change the null value:

(1) Use the nvl function or decode function to convert null to a specific value.

Replace null: nvl (arg, value)

(2) Use case syntax to convert null to a specific value (later than oracle9i. Similar to sqlserver ):

Order by (case mycol when null then 'Beijing drioker' else mycol end)

(3) Use nulls first or nulls last syntax.

Null Value sorting syntax

Nulls first: Place null at the top. For example:

Select *

From mytb

Order by mycol nulls first

Null last: the last line of null. For example:

Select *

From mytb

Order by mycol nulls last

Sqlserver considers null to be the smallest.

Sort in ascending order: null values are ranked first by default.

Order by case when col is null then 1 else 0 end, col

Sort in descending order: the null value is ranked last by default.

Order by case when col is null then 0 else 1 end, col desc

Replace null: isnull (arg, value)

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.