加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

SQL SERVER 如何按当前时间排序

发布时间:2020-12-12 12:32:46 所属栏目:MsSql教程 来源:网络整理
导读:编程之家 jb51.cc 问: 数据库表有一列,表示预约完成时间,此时需要排序 就是 当预约完成的时间是今天的 排在最前,然后是当前时间以后的,然后才是过期的时间数据。不知道我描述清楚没有。。 比如: 3个数据吧 A 时间是 今天 B 时间是明天 C 时间是昨天 取
以下为引用的内容:
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)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

  编程之家 52php.cn

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

  比如: 3个数据吧

  A 时间是 今天

  B 时间是明天

  C 时间是昨天

  取数据时候 排序为 a b c

  请问这样的sql怎么写?

  答:

    推荐文章
      热点阅读