SQL 查询过去时间内的数据

Posted allen-zqw

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL 查询过去时间内的数据相关的知识,希望对你有一定的参考价值。

例如:

  今天的所有数据:select * from 表名 where DateDiff(dd,开始时间,getdate())=0

  昨天的所有数据:select * from 表名 where DateDiff(dd,开始时间,getdate())=1

  7天内的所有数据:select * from 表名 where DateDiff(dd,开始时间,getdate())<=7

  30天内的所有数据:select * from 表名 where DateDiff(dd,开始时间,getdate())<=30

  本月的所有数据:select * from 表名 where DateDiff(mm,开始时间,getdate())=0

  本年的所有数据:select * from 表名 where DateDiff(yy,开始时间,getdate())=0

 

DATEDIFF 函数

DATEDIFF(datepart,startdate,enddate)

startdate 和 enddate 参数是合法的日期表达式。

datepart 参数可以是下列的值:

技术分享图片


以上是关于SQL 查询过去时间内的数据的主要内容,如果未能解决你的问题,请参考以下文章