This is a frequently asked question. Especially if the customer used Oracle before, then he is using SQL When processing the null value, SQL Server and Oracle behave differently.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 However, there is still another solution. You can set the default value of this column to a large value. That is to say, if you cannot provide the value of this column, by default, a large digital generation is used. . If it is int type, the number can be 2147483647. In this case, it is not necessary to judge when sorting. |