我想要排序日期,并使用此代码:
SELECT TID, TName, TOrder
FROM tblOrder
ORDER BY TOrder
结果:
1 maikel null
5 emil null
4 kabol 1
3 samin 2
2 mihad 3
但我想要这个结果:
4 kabol 1
3 samin 2
2 mihad 3
1 maikel null
5 emil null
请帮助我升序排序。
答案 0 :(得分:5)
SELECT TID, TName, TOrder
FROM tblOrder
ORDER BY CASE WHEN TORDER IS NULL THEN 1 ELSE 0 END,
TOrder
的LINQ:
var x = from a in tblOrder
orderby (a.TORDER == null ? 1 : 0), a.TORDER
select a
答案 1 :(得分:0)
尝试使用“ORDER BY TOrder ASC”。
答案 2 :(得分:0)
您可以使用COALESCE
:
SELECT TID, TName, TOrder
FROM tblOrder
ORDER BY COALESCE(TOrder,'9999-12-31 23:59:59.997')
答案 3 :(得分:0)
这是SQL Server最简单的方法:
SELECT TID, TName, TOrder
FROM tblOrder
ORDER BY isnumeric(TOrder) DESC, TOrder
对于MySQL和PostgreSQL等其他数据库,有一个更简单的解决方案:
SELECT TID, TName, TOrder
FROM tblOrder
ORDER BY TOrder IS NULL, TOrder
对于Oracle和PostgreSQL,这有效,并将利用TOrder上可能的索引:
SELECT TID, TName, TOrder
FROM tblOrder
ORDER BY TOrder ASC NULLS LAST
答案 4 :(得分:0)
查看此查询并Fiddle
SELECT TID, TName, TOrder INTO #TempTab
FROM tblOrder
WHERE TOrder IS NOT NULL
ORDER BY TOrder
SELECT * FROM #TempTab
UNION ALL
SELECT TID, TName, TOrder
FROM tblOrder
WHERE TOrder IS NULL;