select * from tbl order by (case when 时间字段<getdate() then -1 else datediff(minute,时间字段,getdate())) desc
--测试用例 declare @t table(t_date varchar(50)) insert into @t select '2005-8-23' union select '2005-10-11' union select '2005-12-23' union select '2005-12-27' union select '2005-12-30'select * from @t order by t_date desc --------------------------------------------- /* 2005-8-23 2005-12-30 2005-12-27 2005-12-23 2005-10-11 */
--测试用例 declare @t table(t_date smalldatetime) insert into @t select '2005-8-23' union select '2005-10-11' union select '2005-12-23' union select '2005-12-27' union select '2005-12-30'select * from @t order by t_date desc --------------------------------------------- /* 2005-12-30 00:00:00 2005-12-27 00:00:00 2005-12-23 00:00:00 2005-10-11 00:00:00 2005-08-23 00:00:00 */
declare @t table(t_date varchar(50)) insert into @t select '2005-8-23' union select '2005-10-11' union select '2005-12-23' union select '2005-12-27' union select '2005-12-30'select* from @t order by month(t_date)desc,day(t_date)asc
select * from tbl order by (case when 时间字段<getdate() then datediff(minute,时间字段,getdate())*-1 else datediff(minute,时间字段,getdate()) end) desc
order by (case when 时间字段<getdate() then -1 else
datediff(minute,时间字段,getdate())) desc
declare @t table(t_date varchar(50))
insert into @t
select '2005-8-23' union
select '2005-10-11' union
select '2005-12-23' union
select '2005-12-27' union
select '2005-12-30'select * from @t order by t_date desc
---------------------------------------------
/*
2005-8-23
2005-12-30
2005-12-27
2005-12-23
2005-10-11
*/
--测试用例
declare @t table(t_date smalldatetime)
insert into @t
select '2005-8-23' union
select '2005-10-11' union
select '2005-12-23' union
select '2005-12-27' union
select '2005-12-30'select * from @t order by t_date desc
---------------------------------------------
/*
2005-12-30 00:00:00
2005-12-27 00:00:00
2005-12-23 00:00:00
2005-10-11 00:00:00
2005-08-23 00:00:00
*/
按照你的做法好象语法错误,提示第一行')'附近语法错误
我的问题好象没有完整,重开一贴
一会结
insert into @t
select '2005-8-23' union
select '2005-10-11' union
select '2005-12-23' union
select '2005-12-27' union
select '2005-12-30'select*
from @t
order by month(t_date)desc,day(t_date)asc
order by (case when 时间字段<getdate() then
datediff(minute,时间字段,getdate())*-1 else
datediff(minute,时间字段,getdate()) end) desc
可以实现了,:)也谢谢各位