How many hours job is executing?

  • Hi list!

    I'm tying

    SELECT name,

    DateDiff(Day, CAST (NEXT_RUN_DATE AS VARCHAR(8)), getdate()) as Days,

    DateDiff(hour,getdate(),CAST (NEXT_RUN_DATE AS VARCHAR(8)) + CAST (NEXT_RUN_TIME AS VARCHAR(6))) as hours

     FROM OpenRowset('SQLOLEDB', 'Server=(local);Trusted_Connection=yes',

        'SET fmtonly OFF

        exec msdb..sp_help_job ') A

    WHERE current_execution_status = 1

    But i got message

    "Syntax error converting datetime from character string"

     

    Thanks in advance for help.

  • NEXT_RUN_DATE is int and not datetime and should be converted to datetime.

    use something like this :

    convert(datetime,substring(cast(run_date as char(8)) ,1,4) + '-' + substring(cast(run_date as char(8)) ,5,2) + '-' + substring(cast(run_date as char(8)) ,7,2) 

             + ' ' +

       substring(cast(RIGHT(run_time + 1000000,6) as char(6)) ,1,2) + ':' + substring(cast(RIGHT(run_time + 1000000,6) as char(6)) ,3,2) + ':' + substring(cast(RIGHT(run_time + 1000000,6) as char(6)) ,5,2)

     &nbsp AS run_DateTime

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me

  • Could you try this :

     

    SELECT name,

    next_run_date,

    next_run_time,

    DateDiff(Day, CAST (next_run_date AS VARCHAR(8)), getdate()) as Days,

    DateDiff(hour,CAST(next_run_date as varchar(8)) +  ' ' + right('00' + CAST((next_run_time/10000)         as varchar(2)),2) + ':' + right('00' + CAST((next_run_time/  100) %   100 as varchar(2)),2),getdate()) as Hours

     FROM OpenRowset('SQLOLEDB', 'Server=(local);Trusted_Connection=yes',

        'SET fmtonly OFF

        exec msdb..sp_help_job ') A

    WHERE current_execution_status = 1

     

  • Thank you very much.

    Both versions work fine.

    I have only one question.

    Why do you use

    (RIGHT(run_time + 1000000,6)

    or

    right('00' + CAST((next_run_time/10000)         as varchar(2)),2) ?

    I tested and it worked fine without all these manipulations , only with fiield "next_run_time".

    There are 6 digits in this field.

     

  • You can also try this one...

    DateDiff(Day, cast(cast(NEXT_RUN_DATE as varchar(8)) as datetime), getdate()) as Days

    and

    DateDiff(hour,getdate(), cast(cast(NEXT_RUN_DATE as varchar(8)) as datetime) + cast(reverse(stuff(stuff(reverse(cast(NEXT_RUN_TIME as varchar(6))), 3, 0, ':'), 6, 0, ':')) as datetime ) ) as hours

     

    HTH...

     

     


    Regards,
    Sachin Dedhia

  • that's to be sure the leading zeroes are in place with hours < 10.

     

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me

Viewing 6 posts - 1 through 5 (of 5 total)

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