Advertisement
elena1234

Populate Calendar Table (T-SQL)

Mar 15th, 2022 (edited)
1,625
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 0.89 KB | None | 0 0
  1. --Update NULL fields in Calendar table
  2.  
  3. UPDATE AdventureWorks2019.dbo.Calendar
  4. SET
  5. DayOfWeekNumber = DATEPART(WEEKDAY,DateValue),
  6. DayOfWeekName = FORMAT(DateValue,'dddd'),
  7. DayOfMonthNumber = DAY(DateValue),
  8. MonthNumber = MONTH(DateValue),
  9. YearNumber = YEAR(DateValue)
  10.  
  11.  
  12. SELECT * FROM AdventureWorks2019.dbo.Calendar
  13.  
  14.  
  15.  
  16. UPDATE AdventureWorks2019.dbo.Calendar
  17. SET
  18. WeekendFlag =
  19.     CASE
  20.         WHEN DayOfWeekNumber IN(1,7) THEN 1
  21.         ELSE 0
  22.     END
  23.  
  24.  
  25. SELECT * FROM AdventureWorks2019.dbo.Calendar
  26.  
  27.  
  28.  
  29. UPDATE AdventureWorks2019.dbo.Calendar
  30. SET
  31. HolidayFlag =
  32.     CASE
  33.         WHEN DayOfMonthNumber = 1 AND MonthNumber = 1 THEN 1
  34.         ELSE 0
  35.     END
  36.  
  37.  
  38. SELECT * FROM AdventureWorks2019.dbo.Calendar
  39.  
  40.  
  41. --Use Calendar table in a query
  42.  
  43.  
  44. SELECT
  45. A.*
  46.  
  47. FROM AdventureWorks2019.Sales.SalesOrderHeader A
  48.     JOIN AdventureWorks2019.dbo.Calendar B
  49.         ON A.OrderDate = B.DateValue
  50.  
  51. WHERE B.WeekendFlag = 1
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement