I have a console app that will choose records based on if the user has chosen to be notified daily at a specific hour of the day as well as all records where the user has chosen hourly.
The job will run every 15 minutes.
How do I find a threshold of time between 5 minutes before and 5 minutes after the top of the hour?
This is my code
Declare @Now datetime
Declare @NowHour int
Declare @NowMinute int
Declare @NewNow nvarchar(50)
Set @Now = {fn Now()}
Set @NowHour = (SELECT DATENAME(hh, @Now))
Set @NowMinute = (SELECT DATENAME(mi, @Now))
Set @NewNow = Cast(Cast(@NowHour As nvarchar(2)) + ':' + Cast(@NowMinute As nvarchar(2)) as Time)
Select @Now, @NowHour, @NowMinute, @NewNow
Select * From vw_consumerAlerts
Where casexid not in
(Select casexid from alerthistory)
And Name = 'Always'
Or ([Hour] Between DateAdd(minute, -5, @NewNow) And DateAdd(minute, -5, @NewNow))
This is the error I’m getting
The data types time and datetime are incompatible in the greater than or equal to operator.
I got it, I had [Hour] and @NewNow swapped, now I get the correct results with:
(@NewNow Between DateAdd(minute, -5, [Hour]) And DateAdd(minute, 5, [Hour]))
Assuming you have defined @TheHour as being the hour in question with 00 minutes/seconds:
UPDATE
See this:
time and datetime are different, so you need to cast your datetime to time as I’ve done in the crude example here.
So in your case: