Time-Sensitive Testing and Debugging

Add default parameter @OVERRIDE_NOW SMALLDATETIME = NULL. Specify a value when testing to set the current time; e.g.

DECLARE

  @TODAY DATE = GETDATE()

, @NOW SMALLDATETIME;

-- By default set @NOW to the current date and time. Specify a value for

-- @OVERRIDE_NOW for testing purposes to show/hide the buckets.

SET @NOW = IIF(@OVERRIDE_NOW IS NULL, GETDATE(), @OVERRIDE_NOW);


-- setup the datetime values for our report buckets

DECLARE 

@STATS_10AM SMALLDATETIME = CONVERT(DATETIME, CONCAT(@STATS_DATE, ' 10:00'))

, @STATS_1PM SMALLDATETIME = CONVERT(DATETIME, CONCAT(@STATS_DATE, ' 13:00'))

, @STATS_4PM SMALLDATETIME = CONVERT(DATETIME, CONCAT(@STATS_DATE, ' 16:00'));


-- Set the current time for each branch based on their time zone and the EST
SELECT
[BranchNumber]
, [Time_Zone]
, CASE
WHEN [Time_Zone] = N'Central' THEN DATEADD(hour, -1, @NOW)
WHEN [Time_Zone] = N'Mountain' THEN DATEADD(hour, -2, @NOW)
WHEN [Time_Zone] = N'Pacific' THEN DATEADD(hour, -3, @NOW)
ELSE @NOW
END AS [BranchCurrentTime]
FROM ...


-- Set indicators in the result set so we know which buckets are getting displayed.
, IIF([BranchCurrentTime] >= @STATS_10AM, 1, 0) AS [SHOW_10AM_BUCKET]
, IIF([BranchCurrentTime] >= @STATS_1PM,  1, 0) AS [SHOW_1PM_BUCKET]
, IIF([BranchCurrentTime] >= @STATS_4PM,  1, 0) AS [SHOW_4PM_BUCKET]


-- Determine which buckets to show on the report; set value to NULL if the bucket should not be rendered
IIF(COALESCE([SHOW_10AM_BUCKET], 1) = 0, NULL, g.LoanSet10) LoanSet10, 
IIF(COALESCE([SHOW_1PM_BUCKET], 1)  = 0, NULL, g.LoanSet13) LoanSet13, 
IIF(COALESCE([SHOW_4PM_BUCKET], 1)  = 0, NULL, g.LoanSet16) LoanSet16,



Comments

Popular posts from this blog

Using sp_executesql with OPENQUERY

Executing Remote Queries Safely and Efficiently with sp_executesql