Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9238729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:49:47+00:00 2026-06-18T07:49:47+00:00

I am trying to solve some issues regarding SQL Server jobs history from last

  • 0

I am trying to solve some issues regarding SQL Server jobs history from last several days with no luck. I like to show job history data as see in Log File Viewer. I run queries and get data but i am confused on how to know which step belongs to job running at particular time. Say if we have a job running every hour and it has 4 steps so when every the job runs it will insert 5 records in sysjobhistory for a successful run now i am concerned about which steps in belongs to which job running at particular time. How i can show it in a drill down report if i want to create one.

SELECT sysjobhistory.server,
         sysjobs.name
         AS
         job_name,
         CASE sysjobhistory.run_status
           WHEN 0 THEN 'Failed'
           WHEN 1 THEN 'Succeeded'
           ELSE '???'
         END
         AS
         run_status,
         Isnull(Substring(CONVERT(VARCHAR(8), run_date), 1, 4) + '-' +
                       Substring(CONVERT(VARCHAR
                                 (8), run_date), 5, 2) + '-' +
                Substring(CONVERT(VARCHAR(
                          8), run_date), 7, 2), '')
         AS
         [Run DATE],
         Isnull(Substring(CONVERT(VARCHAR(7), run_time+1000000), 2, 2) + ':'
                 +
                       Substring(CONVERT(VARCHAR(7), run_time+1000000), 4, 2
                        )
                +
                ':' +
                Substring(CONVERT(VARCHAR(7), run_time+1000000), 6, 2), '')
         AS
         [Run TIME],
         Isnull(Substring(CONVERT(VARCHAR(7), run_duration+1000000), 2, 2) +
                 ':' +
                       Substring(CONVERT(VARCHAR(7), run_duration+1000000),
                       4,
                       2)
                + ':' +
                Substring(CONVERT(VARCHAR(7), run_duration+1000000), 6, 2),
         ''
         ) AS
         [Duration],
         sysjobhistory.step_id,
         sysjobhistory.step_name,
         sysjobhistory.MESSAGE
  FROM   msdb.dbo.sysjobhistory
         INNER JOIN msdb.dbo.sysjobs
           ON msdb.dbo.sysjobhistory.job_id = msdb.dbo.sysjobs.job_id

  ORDER  BY instance_id DESC
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T07:49:48+00:00Added an answer on June 18, 2026 at 7:49 am

    You could try this query. It creates a temp table of jobs based on the step_id = 0 assigning each record a unique identifier. Then it joins back to the job history table using the run time and duration. So, all the steps of one job will have the same RUN_INSTANCE value.

    -- create a temporary table of instances when a job was initiated
    declare @JOBS table
    (
        RUN_INSTANCE uniqueidentifier, 
        job_id uniqueidentifier,
        name sysname,
        run_status int, 
        run_date int, 
        run_time int, 
        run_duration int
    );
    
    -- insert one record for each instanced job and assign it a unique identifier
    insert into @JOBS
        select 
            RUN_INSTANCE = NewID(), 
            h.job_id, 
            j.name, 
            h.run_status, 
            h.run_date, 
            h.run_time, 
            h.run_duration
        from msdb.dbo.sysjobhistory h
            join msdb.dbo.sysjobs j on j.job_id = h.job_id
        where step_id = 0
    
    -- query the jobs history
    select 
        h.server,
        j.RUN_INSTANCE, 
        j.name, 
        h.step_id, 
        h.run_date, 
        h.run_time, 
        run_status = 
            case h.run_status
                when 0 then 'failed'
                when 1 then 'succeeded'
                when 2 then 'retry'
                when 3 then 'canceled'
                when 4 then 'in progress'
                else '???'
            end,
        h.message
    from @JOBS j
        join msdb.dbo.sysjobhistory h on 
            h.job_id = j.job_id
            and convert(varchar(20),h.run_date) + convert(varchar(20),h.run_time) 
               between convert(varchar(20),j.run_date) + convert(varchar(20),j.run_time) 
               and convert(varchar(20),j.run_date) + convert(varchar(20),j.run_time + j.run_duration) 
    order by j.RUN_INSTANCE, h.step_id  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im facing a strange issue trying to move from sql server to oracle. in
Ive been trying to solve some issues for implementing google maps with the api
I've seen some issues posted like this but any answer solve my problem :(
We have some configuration issues that we would like to solve by putting in
I'm inheriting some code here from another developer and I'm trying to solve a
I am new at bash and trying to solve some issues for a code
I'm trying to solve some Google Code Jam problems, where an input matrix is
I'm trying to solve a programming puzzle and running up against some difficulty. It's
I'm trying to get a regexp to solve the next problem: I have some
Trying to solve this problem . I would like to learn how the bootstrapper

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.