SQL SERVER—用于查找特定日期的实用DateTime函数

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL SERVER—用于查找特定日期的实用DateTime函数相关的知识,希望对你有一定的参考价值。

  1. ---Today
  2. SELECT GETDATE() 'Today'
  3. ----Yesterday
  4. SELECT DATEADD(d,-1,GETDATE()) 'Yesterday'
  5. ----First Day of Current Week
  6. SELECT DATEADD(wk,DATEDIFF(wk,0,GETDATE()),0) 'First Day of Current Week'
  7. ----Last Day of Current Week
  8. SELECT DATEADD(wk,DATEDIFF(wk,0,GETDATE()),6) 'Last Day of Current Week'
  9. ----First Day of Last Week
  10. SELECT DATEADD(wk,DATEDIFF(wk,7,GETDATE()),0) 'First Day of Last Week'
  11. ----Last Day of Last Week
  12. SELECT DATEADD(wk,DATEDIFF(wk,7,GETDATE()),6) 'Last Day of Last Week'
  13. ----First Day of Current Month
  14. SELECT DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0) 'First Day of Current Month'
  15. ----Last Day of Current Month
  16. SELECT DATEADD(ms,- 3,DATEADD(mm,0,DATEADD(mm,DATEDIFF(mm,0,GETDATE())+1,0))) 'Last Day of Current Month'
  17. ----First Day of Last Month
  18. SELECT DATEADD(mm,-1,DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0)) 'First Day of Last Month'
  19. ----Last Day of Last Month
  20. SELECT DATEADD(ms,-3,DATEADD(mm,0,DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0))) 'Last Day of Last Month'
  21. ----First Day of Current Year
  22. SELECT DATEADD(yy,DATEDIFF(yy,0,GETDATE()),0) 'First Day of Current Year'
  23. ----Last Day of Current Year
  24. SELECT DATEADD(ms,-3,DATEADD(yy,0,DATEADD(yy,DATEDIFF(yy,0,GETDATE())+1,0))) 'Last Day of Current Year'
  25. ----First Day of Last Year
  26. SELECT DATEADD(yy,-1,DATEADD(yy,DATEDIFF(yy,0,GETDATE()),0)) 'First Day of Last Year'
  27. ----Last Day of Last Year
  28. SELECT DATEADD(ms,-3,DATEADD(yy,0,DATEADD(yy,DATEDIFF(yy,0,GETDATE()),0))) 'Last Day of Last Year'

以上是关于SQL SERVER—用于查找特定日期的实用DateTime函数的主要内容,如果未能解决你的问题,请参考以下文章

sql 判断字段日期和特定日期的大小

SQL Server:查找学生在自定义日期内的连续缺勤计数

如何在 SQL Server 中查找上次修改表的人?

用于查找重叠客户的 SQL 子查询

按日期查找记录sql Server

在 SQL Server 中查找非连续日期