SQLSERVER 查询今天、昨天、本周、上周、本月、上月数据 (转载)

  1. 在做Sql Server开发的时候有时需要获取表中今天、昨天、本周、上周、本月、上月等数据,这时候就需要使用DATEDIFF()函数及GetDate()函数了。
  2. DATEDIFF ( datepart , startdate , enddate )
  3. 释义:计算时间差
  4.  datepare值:year | quarter | month | week | day | hour | minute | second | millisecond 
  5. startdate:开始日期 
  6. enddate :结束日期 
  7. GetDate() 释义:获取当前的系统日期
  8. 下面例子中表名为tablename,条件字段名为inputdate
  9.  查询今天 :SELECT * FROM tablename where DATEDIFF(day,inputdate,GETDATE())=0
  10.  查询昨天:SELECT * FROM tablename where DATEDIFF(day,inputdate,GETDATE())=1
  11.  查询本周:SELECT * FROM tablename where datediff(week,inputdate,getdate())=0
  12.  查询上周:SELECT * FROM tablename where datediff(week,inputdate,getdate())=1
  13.  查询本月:SELECT * FROM tablename where DATEDIFF(month,inputdate,GETDATE())=0
  14.  查询上月:SELECT * FROM tablename where DATEDIFF(month,inputdate,GETDATE())=1
  15.  查询本年:SELECT * FROM Keywords where datediff(year, Addtime,getdate())=0
  16.  转载地址:https://blog.csdn.net/qq285679784/article/details/80426821
posted @ 2020-09-01 13:57  江湖二院院长  阅读(1327)  评论(0编辑  收藏  举报