Advertisement
elena1234

Variables for complex Date math ( T-SQL )

Mar 27th, 2022 (edited)
1,275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 0.41 KB | None | 0 0
  1. --Variables for complex date math:
  2.  
  3. DECLARE @Today DATE = CAST(GETDATE() AS DATE)
  4.  
  5. SELECT @Today
  6.  
  7. DECLARE @BOM DATE = DATEFROMPARTS(YEAR(@Today),MONTH(@Today),1)
  8.  
  9. SELECT @BOM
  10.  
  11. DECLARE @PrevEOM DATE = DATEADD(DAY,-1,@BOM)
  12.  
  13. SELECT @PrevEOM
  14.  
  15. DECLARE @PrevBOM DATE = DATEADD(MONTH,-1,@BOM)
  16.  
  17. SELECT @PrevBOM
  18.  
  19.  
  20.  
  21. SELECT
  22. *
  23. FROM AdventureWorks2019.dbo.Calendar
  24. WHERE DateValue BETWEEN @PrevBOM AND @PrevEOM
  25.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement