Script to calculate when a job ended.

  • I'm trying to format the LAST_RUN_Date and the END_RUN_TIME in the Job history table but I'm not cutting it.

    I hoping to get the DateTime Formated correctly for the Start and the End of the Job Step.

    The commented out part does not work but if you look at you should be able to determine what I'm trying to do.

    I'm filtering on a specific job in this case.

    SELECTj.job_id,

    s.srvname,

    j.name,

    js.step_id,

    js.command,

    j.enabled ,

    last_run_date

    ,

    msdb.dbo.Agent_datetime(last_Run_Date, last_Run_Time) AS Run_Datetime

    --,

    -- Dateadd(second, Datediff(second, 0, msdb.dbo.Agent_datetime(19000101, last_Run_Duration)), msdb.dbo.Agent_datetime(last_Run_Date, last_Run_Time)) AS Run_Endtime

    --Stuff(Stuff(RIGHT('000000'+ Cast(last_Run_Duration AS VARCHAR(6)), 6), 5, 0, ':'), 3, 0, ':') AS run_duration

    FROMdbo.sysjobs j

    JOINdbo.sysjobsteps js

    ONjs.job_id = j.job_id

    JOINmaster.dbo.sysservers s

    ONs.srvid = j.originating_server_id

    WHERE j.name = 'Any Job Name'

    Any help would be greatly appreciated.

    Thanks.

    For better, quicker answers on T-SQL questions, click on the following...
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • Welsh Corgi (3/31/2013)


    I'm trying to format the LAST_RUN_Date and the END_RUN_TIME in the Job history table but I'm not cutting it.

    I'm filtering on a specific job in this case.

    SELECTj.job_id,

    s.srvname,

    j.name,

    js.step_id,

    js.command,

    j.enabled ,

    last_run_date

    ,

    msdb.dbo.Agent_datetime(last_Run_Date, last_Run_Time) AS Run_Datetime

    --,

    -- Dateadd(second, Datediff(second, 0, msdb.dbo.Agent_datetime(19000101, last_Run_Duration)), msdb.dbo.Agent_datetime(last_Run_Date, last_Run_Time)) AS Run_Endtime

    --Stuff(Stuff(RIGHT('000000'+ Cast(last_Run_Duration AS VARCHAR(6)), 6), 5, 0, ':'), 3, 0, ':') AS run_duration

    FROMdbo.sysjobs j

    JOINdbo.sysjobsteps js

    ONjs.job_id = j.job_id

    JOINmaster.dbo.sysservers s

    ONs.srvid = j.originating_server_id

    WHERE j.name = 'Any Job Name'

    Any help would be greatly appreciated.

    Thanks.

    So, what is the problem?

  • Try executing the query.

    The code does not do what I intended.

    For better, quicker answers on T-SQL questions, click on the following...
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • I can run the code all day long, it won't tell me what the problem is if you don't show me. I have no idea what the expected results of the query are to compare the actual results.

  • Lynn Pettis (3/31/2013)


    I can run the code all day long, it won't tell me what the problem is if you don't show me. I have no idea what the expected results of the query are to compare the actual results.

    Since you do not have any job history you can't test. But think about.

    I had a situation a few weeks ago when I setup up a new server and I needed to test the job notification on failure so I created a job and added a step with the sp_who3 command mistyped and I was able to successfully test the job.

    Create a job with a sp_who2 command and execute my code.

    If you do not feel like it then please disregard my post.

    For better, quicker answers on T-SQL questions, click on the following...
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • Lynn Pettis (3/10/2013)


    Welsh Corgi (3/10/2013)


    Lynn Pettis (3/10/2013)


    Since some of us do not necessarily have a similar environment as you, it may be helpful if you created temporary tables and populated it with data from your msdb database for us to work with.

    A little hard to work on your code with no data to test against. Won't know if what we did was what you are actually trying to accomplish.

    From your own signature block:

    For better, quicker answers on T-SQL questions, click on the following...

    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    You know it is a system table why do I need to create a system table?:w00t:

    Thank you.

    How about for the fact some of us don't necessarily have data in those tables? Most of my msdb database tables are empty, I have no scheduled jobs running, nor do I have any jobs that may even look like yours. My VM at work is the same, no jobs, no data. As for the production systems, on secure networks in theater so no access.

    I also don't feel compelled to develop test data to help you do your work. I am more than happy to help, but give us something to work with.

    You can't create a job and execute it so that you will have data to test?

    For better, quicker answers on T-SQL questions, click on the following...
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • Welsh Corgi (3/31/2013)


    Lynn Pettis (3/10/2013)


    Welsh Corgi (3/10/2013)


    Lynn Pettis (3/10/2013)


    Since some of us do not necessarily have a similar environment as you, it may be helpful if you created temporary tables and populated it with data from your msdb database for us to work with.

    A little hard to work on your code with no data to test against. Won't know if what we did was what you are actually trying to accomplish.

    From your own signature block:

    For better, quicker answers on T-SQL questions, click on the following...

    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    You know it is a system table why do I need to create a system table?:w00t:

    Thank you.

    How about for the fact some of us don't necessarily have data in those tables? Most of my msdb database tables are empty, I have no scheduled jobs running, nor do I have any jobs that may even look like yours. My VM at work is the same, no jobs, no data. As for the production systems, on secure networks in theater so no access.

    I also don't feel compelled to develop test data to help you do your work. I am more than happy to help, but give us something to work with.

    You can't create a job and execute it so that you will have data to test?

    Yes, I can. But all that is going to show me is what your query returns. What is it you WANT it to return? You need to show me that.

  • Lynn Pettis (3/31/2013)


    Welsh Corgi (3/31/2013)


    Lynn Pettis (3/10/2013)


    Welsh Corgi (3/10/2013)


    Lynn Pettis (3/10/2013)


    Since some of us do not necessarily have a similar environment as you, it may be helpful if you created temporary tables and populated it with data from your msdb database for us to work with.

    A little hard to work on your code with no data to test against. Won't know if what we did was what you are actually trying to accomplish.

    From your own signature block:

    For better, quicker answers on T-SQL questions, click on the following...

    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    You know it is a system table why do I need to create a system table?:w00t:

    Thank you.

    How about for the fact some of us don't necessarily have data in those tables? Most of my msdb database tables are empty, I have no scheduled jobs running, nor do I have any jobs that may even look like yours. My VM at work is the same, no jobs, no data. As for the production systems, on secure networks in theater so no access.

    I also don't feel compelled to develop test data to help you do your work. I am more than happy to help, but give us something to work with.

    You can't create a job and execute it so that you will have data to test?

    Yes, I can. But all that is going to show me is what your query returns. What is it you WANT it to return? You need to show me that.

    I want the following colums displayed in a valid date format:

    This is what I get:

    last_run_datelast_Run_Time

    20130331101254

    I want the last_run_date and last_run_date & last_end_Date formated as follows:

    2013-03-31 03:25:00.0002013-03-31 03:37:33.000

    If you refer to the code for the sysjobs it has the logice and the expected output.

    I can't get it to work for the sysjobstep.

    For better, quicker answers on T-SQL questions, click on the following...
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • Perhaps something more like this:

    SELECT

    j.job_id,

    s.srvname,

    j.name,

    js.step_id,

    js.command,

    j.enabled ,

    js.last_run_date,

    js.last_Run_Time,

    js.last_run_duration,

    msdb.dbo.Agent_datetime(js.last_Run_Date, js.last_Run_Time) AS Run_Datetime,

    dateadd(second, ((js.last_run_duration / 10000) * 3600) + (((js.last_run_duration / 100) % 60) * 60) + (js.last_run_duration % 60), msdb.dbo.Agent_datetime(js.last_Run_Date, js.last_Run_Time)) as EndRunDate

    FROM

    dbo.sysjobs j

    JOIN dbo.sysjobsteps js

    ONjs.job_id = j.job_id

    JOIN master.dbo.sysservers s

    ONs.srvid = j.originating_server_id

    WHERE

    j.name = 'TestJob';

  • Lynn Pettis (3/31/2013)


    Perhaps something more like this:

    SELECT

    j.job_id,

    s.srvname,

    j.name,

    js.step_id,

    js.command,

    j.enabled ,

    js.last_run_date,

    js.last_Run_Time,

    js.last_run_duration,

    msdb.dbo.Agent_datetime(js.last_Run_Date, js.last_Run_Time) AS Run_Datetime,

    dateadd(second, ((js.last_run_duration / 10000) * 3600) + (((js.last_run_duration / 100) % 60) * 60) + (js.last_run_duration % 60), msdb.dbo.Agent_datetime(js.last_Run_Date, js.last_Run_Time)) as EndRunDate

    FROM

    dbo.sysjobs j

    JOIN dbo.sysjobsteps js

    ONjs.job_id = j.job_id

    JOIN master.dbo.sysservers s

    ONs.srvid = j.originating_server_id

    WHERE

    j.name = 'TestJob';

    If you execute the code you will get the following error:

    Msg 241, Level 16, State 1, Line 2

    Conversion failed when converting date and/or time from character string.

    Thanks for you reply.

    For better, quicker answers on T-SQL questions, click on the following...
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • Welsh Corgi (3/31/2013)


    Lynn Pettis (3/31/2013)


    Perhaps something more like this:

    SELECT

    j.job_id,

    s.srvname,

    j.name,

    js.step_id,

    js.command,

    j.enabled ,

    js.last_run_date,

    js.last_Run_Time,

    js.last_run_duration,

    msdb.dbo.Agent_datetime(js.last_Run_Date, js.last_Run_Time) AS Run_Datetime,

    dateadd(second, ((js.last_run_duration / 10000) * 3600) + (((js.last_run_duration / 100) % 60) * 60) + (js.last_run_duration % 60), msdb.dbo.Agent_datetime(js.last_Run_Date, js.last_Run_Time)) as EndRunDate

    FROM

    dbo.sysjobs j

    JOIN dbo.sysjobsteps js

    ONjs.job_id = j.job_id

    JOIN master.dbo.sysservers s

    ONs.srvid = j.originating_server_id

    WHERE

    j.name = 'TestJob';

    If you execute the code you will get the following error:

    Msg 241, Level 16, State 1, Line 2

    Conversion failed when converting date and/or time from character string.

    Thanks for you reply.

    Runs just fine on my system. Perhaps now is the time you should consider my original advise and create a mirror of your system tables, insert some of your data and let us try against what you are running against.

  • I just did a copy/paste/execute of the code I posted in a new SSMS query window, no problems.

  • Welsh Corgi (3/31/2013)


    Lynn Pettis (3/31/2013)


    Perhaps something more like this:

    SELECT

    j.job_id,

    s.srvname,

    j.name,

    js.step_id,

    js.command,

    j.enabled ,

    js.last_run_date,

    js.last_Run_Time,

    js.last_run_duration,

    msdb.dbo.Agent_datetime(js.last_Run_Date, js.last_Run_Time) AS Run_Datetime,

    dateadd(second, ((js.last_run_duration / 10000) * 3600) + (((js.last_run_duration / 100) % 60) * 60) + (js.last_run_duration % 60), msdb.dbo.Agent_datetime(js.last_Run_Date, js.last_Run_Time)) as EndRunDate

    FROM

    dbo.sysjobs j

    JOIN dbo.sysjobsteps js

    ONjs.job_id = j.job_id

    JOIN master.dbo.sysservers s

    ONs.srvid = j.originating_server_id

    WHERE

    j.name = 'TestJob';

    If you execute the code you will get the following error:

    Msg 241, Level 16, State 1, Line 2

    Conversion failed when converting date and/or time from character string.

    Thanks for you reply.

    What job did you create?

    What was in the execute of the step(s)?

    For better, quicker answers on T-SQL questions, click on the following...
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • Welsh Corgi (3/31/2013)


    Welsh Corgi (3/31/2013)


    Lynn Pettis (3/31/2013)


    Perhaps something more like this:

    SELECT

    j.job_id,

    s.srvname,

    j.name,

    js.step_id,

    js.command,

    j.enabled ,

    js.last_run_date,

    js.last_Run_Time,

    js.last_run_duration,

    msdb.dbo.Agent_datetime(js.last_Run_Date, js.last_Run_Time) AS Run_Datetime,

    dateadd(second, ((js.last_run_duration / 10000) * 3600) + (((js.last_run_duration / 100) % 60) * 60) + (js.last_run_duration % 60), msdb.dbo.Agent_datetime(js.last_Run_Date, js.last_Run_Time)) as EndRunDate

    FROM

    dbo.sysjobs j

    JOIN dbo.sysjobsteps js

    ONjs.job_id = j.job_id

    JOIN master.dbo.sysservers s

    ONs.srvid = j.originating_server_id

    WHERE

    j.name = 'TestJob';

    If you execute the code you will get the following error:

    Msg 241, Level 16, State 1, Line 2

    Conversion failed when converting date and/or time from character string.

    Thanks for you reply.

    What job did you create?

    What was in the execute of the step(s)?

    You can't answer my questions or provide the information I request and you expect me to answer yours?

    It doesn't matter what job I created or what I did in the job step(s). The job ran and put data in the tables that when accessed by your query with my added code to compute the End Date ran without problems.

    This means there is more going on on your system and that we need to see some data from you in order to help answer your questions. I am more than willing to help, but you really need to help in that regard.

  • Lynn Pettis (3/31/2013)


    Welsh Corgi (3/31/2013)


    Welsh Corgi (3/31/2013)


    Lynn Pettis (3/31/2013)


    Perhaps something more like this:

    SELECT

    j.job_id,

    s.srvname,

    j.name,

    js.step_id,

    js.command,

    j.enabled ,

    js.last_run_date,

    js.last_Run_Time,

    js.last_run_duration,

    msdb.dbo.Agent_datetime(js.last_Run_Date, js.last_Run_Time) AS Run_Datetime,

    dateadd(second, ((js.last_run_duration / 10000) * 3600) + (((js.last_run_duration / 100) % 60) * 60) + (js.last_run_duration % 60), msdb.dbo.Agent_datetime(js.last_Run_Date, js.last_Run_Time)) as EndRunDate

    FROM

    dbo.sysjobs j

    JOIN dbo.sysjobsteps js

    ONjs.job_id = j.job_id

    JOIN master.dbo.sysservers s

    ONs.srvid = j.originating_server_id

    WHERE

    j.name = 'TestJob';

    If you execute the code you will get the following error:

    Msg 241, Level 16, State 1, Line 2

    Conversion failed when converting date and/or time from character string.

    Thanks for you reply.

    What job did you create?

    What was in the execute of the step(s)?

    You can't answer my questions or provide the information I request and you expect me to answer yours?

    It doesn't matter what job I created or what I did in the job step(s). The job ran and put data in the tables that when accessed by your query with my added code to compute the End Date ran without problems.

    This means there is more going on on your system and that we need to see some data from you in order to help answer your questions. I am more than willing to help, but you really need to help in that regard.

    I asked you for that information so that I could replicate what you did .

    Disregard my post

    Thank you.

    For better, quicker answers on T-SQL questions, click on the following...
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

Viewing 15 posts - 16 through 30 (of 42 total)

You must be logged in to reply to this topic. Login to reply