I want write a query to get the last 24 hours worth of job record from the \"msdb.dbo.sysjobhistory\" table, but I can\'t get because I get the \"run_date\" and \"run_time\"
A co-worker of mine pointed out that the other given solutions only find jobs that we're started 24 hours ago (or less), not jobs that were completed 24 hours ago (or less). He proposed something along the lines of the following to location completed jobs:
SELECT j.name AS JobName
,LastCompletedDateTime = DATEADD(day, (run_duration / 240000), CONVERT(DATETIME, msdb.dbo.agent_datetime(run_date, run_time))) +
STUFF(STUFF(REPLACE(STR((run_duration % 240000), 7, 0), ' ', '0'), 4, 0, ':'), 7, 0, ':')
FROM msdb..sysjobs j
INNER JOIN msdb..sysjobhistory jh ON j.job_id = jh.job_id
WHERE DATEADD(day, (run_duration / 240000), CONVERT(DATETIME, msdb.dbo.agent_datetime(run_date, run_time))) +
STUFF(STUFF(REPLACE(STR((run_duration % 240000), 7, 0), ' ', '0'), 4, 0, ':'), 7, 0, ':') > DATEADD(HOUR, - 24, GETDATE())
I believe part of this comes from a blog, but we cannot locate it, when I do I'll link that as well...