Advertisement
makispaiktis

Query 6 - Date Functions (GETDATE, DATEADD, DATEDIFF)

Jun 6th, 2023 (edited)
1,269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 0.70 KB | Source Code | 0 0
  1. USE [AdventureWorks2012]
  2.  
  3. -- Now date
  4. SELECT GETDATE()
  5. -- 2 days before
  6. SELECT GETDATE()-2
  7.  
  8. -- DATEPART
  9. SELECT DATEPART(yyyy, GETDATE()) AS YearNumber
  10. SELECT DATEPART(mm, GETDATE()) AS MonthNumber
  11. SELECT DATEPART(dd, GETDATE()) AS DayNumber
  12.  
  13. -- DATEADD
  14. SELECT DATEADD(DAY, 4, GETDATE())
  15. SELECT DATEADD(MONTH, 4, GETDATE())
  16. SELECT DATEADD(YEAR, 10, GETDATE())
  17. SELECT DATEADD(YEAR, -10, '12/27/2040')
  18.  
  19. -- The 1st day of the month
  20. SELECT DATEADD(dd, -DATEPART(DAY, GETDATE()-1), GETDATE())
  21.  
  22. -- Simple Date Queries
  23. -- DATEDIFF
  24. SELECT TOP 10 * FROM [Production].[WorkOrder]
  25. SELECT TOP 10 WorkOrderID, ProductID, StartDate, EndDate, DATEDIFF(DAY, startDate, EndDate)
  26. FROM [Production].[WorkOrder]
  27.  
  28.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement