Okay, so I have a question about using columns in a case statement that might be null.
Again I have two tables the first looks like this UserActivity:
userID Action Time
1 25 12:00
1 10 12:01
1 12 12:35
1 6 13:54
2 10 6:47
2 42 6:48
3 8 11:54
etc.
But the second now looks like this UserSchedule:
userID startTime1 stopTime1 startTime2 stoptime2 startTime3 stopTime3
1 07:00 09:00 11:00 12:00 14:30 16:30
2 11:00 12:30 14:00 15:30
3 14:00 15:00
etc.
So I need to be able to evaluate when in relation to all of those start and stop times an Action took place. Some users will have up to 5 start/stop times in a day. Some will only have one.
This code was given to me to resolve an earlier issue by @Adam Wengler, and it works great if there is only one start and stop time. I’m unsure how to include the logic to test against each of the 5 potential start/stop columns and ignore them if they are empty.
UPDATE ua
SET ua.TimeStatusId = CASE
WHEN ua.Time >= us.Schedule_Start
AND ua.Time <= us.Schedule_stop THEN 1
WHEN ua.Time >= DATEADD(HOUR, -1, us.Schedule_Start)
AND ua.Time <= us.ScheduleStart THEN 0
WHEN ua.Time >= us.Schedule_Stop
AND ua.Time =< DATEADD(HOUR, 1, us.Schedule_Stop)
THEN 2
ELSE 3
END
FROM dbo.UserActivity AS ua
INNER JOIN dbo.userSchedule AS us ON ua.UserId = us.UserId
You can use the
IsNull()function to resolve your issue