简体   繁体   English

在SQL Server中按日期排序

[英]Sorting by date in SQL Server

I want to sort a table by date in ascending order. 我想按日期升序对表格进行排序。 The table also contains null values. 该表还包含空值。

I want to display data in Gridview. 我想在Gridview中显示数据。 I want to display the data like first all rows which contains date field and then date as blank field. 我想像首先显示所有包含日期​​字段的行,然后将日期显示为空白字段一样显示数据。

I used below query : 我用下面的查询:

select * from TempTable order by convert(datetime, Date,101) asc

Please help me. 请帮我。

Thanks in Advance. 提前致谢。

Then you should check for null values: 然后,您应该检查null值:

Select * FROM TempTable 
ORDER BY CASE WHEN datetime IS NULL THEN 1 ELSE 0 END ASC 
,  Date ASC
select * from TempTable 
order by case when [Date] is null then 1 else 0 end, [Date]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM