Search code examples
sqlsql-serversql-server-2000

Get the last 24 hour job record form msdb.dbo.sysjobhistory


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" columns are returned as a number. How can I convert the "run_date" and "run_time" columns into a datetime variable, and use this to get the last 24 hour job history?


Solution

  • Check out this post - it shows how to "decode" those run_date columns from sysjobhistory.

    You should be able to get the entries from the last 24 hours with a query something like this:

    SELECT 
        j.name as JobName, 
        LastRunDateTime = 
        CONVERT(DATETIME, CONVERT(CHAR(8), run_date, 112) + ' ' 
        + STUFF(STUFF(RIGHT('000000' + CONVERT(VARCHAR(8), run_time), 6), 5, 0, ':'), 3, 0, ':'), 121)
    FROM 
        msdb..sysjobs j
    INNER JOIN
        msdb..sysjobhistory jh ON j.job_id = jh.job_id
    WHERE
        CONVERT(DATETIME, CONVERT(CHAR(8), run_date, 112) + ' ' 
        + STUFF(STUFF(RIGHT('000000' + CONVERT(VARCHAR(8), run_time), 6), 5, 0, ':'), 3, 0, ':'), 121) > DATEADD(HOUR, -24, GETDATE())