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