国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > 数据库 > Sqlserver > SQL SERVER 如何按当前时间排序

SQL SERVER 如何按当前时间排序

来源:程序员人生   发布时间:2013-11-21 13:25:49 阅读次数:2644次

  网(LieHuo.Net)教程

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

  比如: 3个数据吧

  A 时间是 今天

  B 时间是明天

  C 时间是昨天

  取数据时候 排序为 a b c

  请问这样的sql怎么写?

  答:

以下为引用的内容:
Create table Testtable(ID int identity(1,1),dt datetime)goinsert into Testtable(dt)select dateadd(dd,2,GETDATE())union allselect GETDATE()union allselect dateadd(dd,-2,GETDATE())union allselect 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 @tommorrowselect * from Testtable where dt between cast(@today as datetime) and cast(@tommorrow as datetime) union allselect * from Testtable where dt>=cast(@tommorrow as datetime)union allselect * from Testtable where dt<=cast(@today as datetime)

生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生