Null values are ranked at the end of SQL Server sorting

Source: Internet
Author: User

Recently encountered a sort of SQL Server problem, have not known before, and then this time met.

To find the problem with SQL Server sorting, the null value is the default in the specified column, because the null value defaults to the minimum value in SQL Server.

Later asked the almighty Baidu, found the following a relatively simple processing method.

First, a table was built to insert some test data.

Create TableUserInfo (userinfoidint  not NULL Identity(1,1)Primary Key, User_noint NULL, User_namesnvarchar( -)NULL)Insert  intoUserInfo (user_no,user_names)Select '104','name three' Union  AllSelect '103','name Two' Union  AllSelect '108','Name VII' Union  AllSelect ' the','name Four' Union  AllSelect '106','name Five' Union  AllSelect '102','Name one' Union  AllSelect '107','name Six' Union  AllSelect '109','name Eight' Insert  intoUserInfo (user_names)Select 'name Nine' Union  AllSelect 'name 10'Select *  fromUserInfo

The following first direct sorting to see the effect.

 Select from ORDER by ASC

You can see the column with the specified sort, with a value of NULL at the front.

Here's the solution.

 Select from order by case  Nullthen1else0endASCASC

The above is the solution, not only the value of the specified row sequence of NULL at the end of the row, you can also follow the specified column values to sort, is not very simple.

Null values are ranked at the end of SQL Server sorting

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.