SQL Server 2005: Order with NULL values at the end [duplicate]

寵の児 提交于 2019-12-23 07:20:06

问题


Possible Duplicate:
Case Order by using Null

I'm looking to get a list of records ordered by an "ordernum" field. The ordernum field is an int field. This field starts as NULL until set by a user. I would like the NULL entries to appear at the end of the list.

I am building a query as follows:

select *, case when (ordernum is null) then [largestInt] else ordernum end as newordernum
from tableName
order by newordernum

I know I could enter the value for the largest possible int for [largestInt], but I would like to replace [largestInt] with a variable. Is this possible?


回答1:


I found a way to order NULL values on the bottom.

http://sqlblog.com/blogs/denis_gobo/archive/2007/10/19/3048.aspx

It meets my needs quite nicely. My query is now:

select *
from tableName
order by case when ordernum is null then 1 else 0 end, ordernum


来源:https://stackoverflow.com/questions/11333863/sql-server-2005-order-with-null-values-at-the-end

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!