且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

SQL Server 中的排序顺序

更新时间:2023-01-17 19:11:29

您几乎可以使用 caseorder by 中进行任何排序.首先是 null 列,然后是空字符串,其余的列在 col1 和 col3 上:

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