首页 新闻 赞助 找找看

sql 按当前时间排序问题

0
悬赏园豆:5 [已解决问题] 解决于 2009-09-01 13:55

 数据库表有一列,表示预约完成时间,此时需要排序  就是  当预约完成的时间是今天的 排在最前,然后是当前时间以后的,然后才是过期的时间数据。不知道我描述清楚没有。。

比如: 3个数据吧

A    时间是 今天

B   时间是明天

C 时间是昨天

取数据时候 排序为  a  b c

请问这样的sql怎么写?

越~、。的主页 越~、。 | 初学一级 | 园豆:0
提问于:2009-09-01 11:18
< >
分享
最佳答案
0

Create table  Testtable
(ID
int identity(1,1),
dt
datetime)
go

insert into Testtable(dt)
select dateadd(dd,2,GETDATE())
union all
select GETDATE()
union all
select dateadd(dd,-2,GETDATE())
union all
select GETDATE()

declare @today varchar(20)
set @today=CONVERT (VARCHAR(10),GETDATE(),121)
declare @tommorrow varchar(20)
set @tommorrow=CONVERT (VARCHAR(10),dateadd(dd,1,GETDATE()),121)
--select @today
--
select @tommorrow

select * from Testtable where dt between cast(@today as datetime) and cast(@tommorrow as datetime)
union all
select * from Testtable where dt>=cast(@tommorrow as datetime)
union all
select * from Testtable where dt<=cast(@today as datetime)


执行结果

ID dt
2 2009-09-01 11:40:12.013
4 2009-09-01 11:40:12.013
1 2009-09-03 11:40:12.013
3 2009-08-30 11:40:12.013

收获园豆:5
邀月 | 高人七级 |园豆:25475 | 2009-09-01 11:59
xiexie
越~、。 | 园豆:0 (初学一级) | 2009-09-01 13:55
其他回答(1)
0

order by 预约时间。asc  |desc 

邢少 | 园豆:10926 (专家六级) | 2009-09-01 11:59
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册