Advertisement
DataCCIW

Seasonal Address Activation

Dec 23rd, 2019
799
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 3.37 KB | None | 0 0
  1. DECLARE @AddressTypeHome INT = 6;
  2.  
  3. WITH Address_With_INT_Dates
  4.  
  5. AS(
  6. SELECT *,
  7.     today_month_day_INT = CONVERT(INT, REPLACE(CONVERT(VARCHAR (5), GETDATE(), 1),'/', '')),
  8.     from_month_day_INT = CONVERT(INT, PA.from_month_day),
  9.     to_month_day_INT  = CONVERT(INT, PA.to_month_day)
  10. FROM core_person_address PA
  11. )
  12.  
  13. UPDATE CPA -- Removing primary address flag to allow setting seasonal address as primary
  14. SET CPA.primary_address = 0
  15. FROM Address_With_INT_Dates PA
  16. JOIN core_person_address CPA ON CPA.person_id = PA.person_id
  17.     AND CPA.primary_address = 1
  18. WHERE PA.from_month_day != 0000
  19. AND PA.primary_address = 0
  20. AND
  21. (
  22.    (to_month_day_INT > from_month_day_INT
  23.       AND today_month_day_INT >= from_month_day_INT
  24.       AND today_month_day_INT <= to_month_day_INT
  25.    )
  26. OR
  27.    (to_month_day_INT < from_month_day_INT
  28.       AND (today_month_day_INT >= from_month_day_INT
  29.             OR today_month_day_INT <= to_month_day_INT
  30.           )
  31.    )
  32. );
  33.  
  34. WITH Address_With_INT_Dates
  35.  
  36. AS(
  37. SELECT *,
  38.     today_month_day_INT = CONVERT(INT, REPLACE(CONVERT(VARCHAR (5), GETDATE(), 1),'/', '')),
  39.     from_month_day_INT = CONVERT(INT, PA.from_month_day),
  40.     to_month_day_INT  = CONVERT(INT, PA.to_month_day)
  41. FROM core_person_address PA
  42. )
  43.  
  44.  
  45. UPDATE PA -- Set seasonal address as primary
  46. SET PA.primary_address = 1
  47. FROM Address_With_INT_Dates PA
  48. WHERE PA.from_month_day != 0000
  49. AND PA.primary_address = 0
  50. AND
  51. (
  52.    (to_month_day_INT > from_month_day_INT
  53.       AND today_month_day_INT >= from_month_day_INT
  54.       AND today_month_day_INT <= to_month_day_INT
  55.    )
  56. OR
  57.    (to_month_day_INT < from_month_day_INT
  58.       AND (today_month_day_INT >= from_month_day_INT
  59.             OR today_month_day_INT <= to_month_day_INT
  60.           )
  61.    )
  62. );
  63.  
  64. WITH Address_With_INT_Dates
  65.  
  66. AS(
  67. SELECT *,
  68.     today_month_day_INT = CONVERT(INT, REPLACE(CONVERT(VARCHAR (5), GETDATE(), 1),'/', '')),
  69.     from_month_day_INT = CONVERT(INT, PA.from_month_day),
  70.     to_month_day_INT  = CONVERT(INT, PA.to_month_day)
  71. FROM core_person_address PA
  72. )
  73.  
  74. -- Removing primary address tag from no longer active seasonal address
  75. UPDATE PA
  76. SET PA.primary_address = 0
  77. FROM Address_With_INT_Dates PA
  78. WHERE PA.from_month_day != 0000
  79. AND PA.primary_address = 1
  80. AND
  81. (
  82.    (to_month_day_INT > from_month_day_INT
  83.       AND (today_month_day_INT < from_month_day_INT
  84.             OR today_month_day_INT > to_month_day_INT
  85.           )
  86.    )
  87. OR
  88.    (to_month_day_INT < from_month_day_INT
  89.       AND (today_month_day_INT < from_month_day_INT
  90.             AND today_month_day_INT > to_month_day_INT
  91.           )
  92.    )
  93. );
  94.  
  95.  
  96.  
  97. WITH Address_With_INT_Dates
  98.  
  99. AS(
  100. SELECT *,
  101.     today_month_day_INT = CONVERT(INT, REPLACE(CONVERT(VARCHAR (5), GETDATE(), 1),'/', '')),
  102.     from_month_day_INT = CONVERT(INT, PA.from_month_day),
  103.     to_month_day_INT  = CONVERT(INT, PA.to_month_day)
  104. FROM core_person_address PA
  105. )
  106.  
  107. -- Finding Person's Main Home Address and setting it Primary
  108. UPDATE CPA
  109. SET CPA.primary_address = 1
  110. FROM Address_With_INT_Dates PA
  111. JOIN core_person_address CPA ON CPA.person_id = PA.person_id
  112. AND CPA.primary_address = 0
  113. AND CPA.address_type_luid = @AddressTypeHome
  114. WHERE PA.primary_address = 0
  115. AND (
  116.    (to_month_day_INT > from_month_day_INT
  117.       AND (today_month_day_INT < from_month_day_INT
  118.             OR today_month_day_INT > to_month_day_INT
  119.           )
  120.    )
  121. OR
  122.    (to_month_day_INT < from_month_day_INT
  123.       AND (today_month_day_INT < from_month_day_INT
  124.             AND today_month_day_INT > to_month_day_INT
  125.           )
  126.    )
  127. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement