Sort Order in SQL Server

て烟熏妆下的殇ゞ 提交于 2019-12-24 06:26:31

问题


Can I change the default sort order in a SQL server database so that nulls and zero length strings are displayed last.

Its SQL Server 2000

I stress, I want to change the default order for all queries, if possible


回答1:


You can do almost any sort using a case in an order by. Here's the null columns first, then the empty strings, and the rest ordered on col1 and col3:

select  *
from    YourTable
order by
        case when col1 is null then 1
             when col1 = '' then 2
             else 3
        end
,       col2
,       col3 desc



回答2:


No you cannot do that: Without ORDER BY, there is no default sort order. This is a very common question, so I wrote a canned answer: Without ORDER BY, there is no default sort order




回答3:


add a dummy newcolumn = (length(targetcolumn)>0), and sort by this first.



来源:https://stackoverflow.com/questions/3574284/sort-order-in-sql-server

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