Posted By

kashif21 on 04/14/11


sql server functions to DateTime find dates useful specific

Versions (?)

Who likes this?

1 person have marked this snippet as a favorite


SQL SERVER – Useful DateTime Functions to Find Specific Dates

 / Published in: SQL

  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'

Report this snippet  

You need to login to post a comment.