SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


DBO.SysJobHistory, how do you get the Job Time?


DBO.SysJobHistory, how do you get the Job Time?

Author
Message
dwilliscp
dwilliscp
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2215 Visits: 784
In the MSDB MS is storing the information about a SQL Job, and in the DBO.SysJobHistory it has a column called "run_time".. Using SQL how do you convert this field to get the time the job ran?
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)

Group: General Forum Members
Points: 97984 Visits: 38995
dwilliscp (5/1/2012)
In the MSDB MS is storing the information about a SQL Job, and in the DBO.SysJobHistory it has a column called "run_time".. Using SQL how do you convert this field to get the time the job ran?


Are you asking at what time the job ran or how long the job (or job step) ran?

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)

Group: General Forum Members
Points: 97984 Visits: 38995
If what time, the integer value represents the time the job or job step started in the format hhmmss.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Robin Sasson
Robin Sasson
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1153 Visits: 530
Hi Lynn,

I can't remember where I got hold of this script but it does the conversion as you've asked:


SELECT J.[name] JobName
, J.[description] JobDescription
, CONVERT(DATETIME, Cast(S.next_run_date AS VARCHAR), 112) AS NextRunDate
, Datename(dw, CONVERT(DATETIME, Cast(S.next_run_date AS VARCHAR), 112)) AS NextRunDate_Day
, S.next_run_time AS NextRunTime
, (SELECT Min(run_time)
FROM msdb.dbo.sysjobhistory H
WHERE H.job_id = J.job_id) AS Duration_MIN
, (SELECT Avg(run_time)
FROM msdb.dbo.sysjobhistory H
WHERE H.job_id = J.job_id) AS Duration_AVG
, (SELECT Max(run_time)
FROM msdb.dbo.sysjobhistory H
WHERE H.job_id = J.job_id) AS Duration_MAX
FROM msdb.dbo.sysjobs J
LEFT JOIN msdb.dbo.sysjobschedules S
ON J.job_id = S.job_id
WHERE J.[Enabled] = 1

Exclamation "Be brave. Take risks. Nothing can substitute experience." Exclamation
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)

Group: General Forum Members
Points: 97984 Visits: 38995
Robin Sasson (5/1/2012)
Hi Lynn,

I can't remember where I got hold of this script but it does the conversion as you've asked:


SELECT J.[name] JobName
, J.[description] JobDescription
, CONVERT(DATETIME, Cast(S.next_run_date AS VARCHAR), 112) AS NextRunDate
, Datename(dw, CONVERT(DATETIME, Cast(S.next_run_date AS VARCHAR), 112)) AS NextRunDate_Day
, S.next_run_time AS NextRunTime
, (SELECT Min(run_time)
FROM msdb.dbo.sysjobhistory H
WHERE H.job_id = J.job_id) AS Duration_MIN
, (SELECT Avg(run_time)
FROM msdb.dbo.sysjobhistory H
WHERE H.job_id = J.job_id) AS Duration_AVG
, (SELECT Max(run_time)
FROM msdb.dbo.sysjobhistory H
WHERE H.job_id = J.job_id) AS Duration_MAX
FROM msdb.dbo.sysjobs J
LEFT JOIN msdb.dbo.sysjobschedules S
ON J.job_id = S.job_id
WHERE J.[Enabled] = 1



???

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
dwilliscp
dwilliscp
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2215 Visits: 784
SELECT RUN_TIME
FROM MSDB.DBO.SYSJOBHISTORY

I have run_times from 200 up to 230200??? The run_date is YYYYMMDD but how you get the time the job ran from 200 or 230200 I have no idea.
rwlp.french
rwlp.french
SSC Journeyman
SSC Journeyman (94 reputation)SSC Journeyman (94 reputation)SSC Journeyman (94 reputation)SSC Journeyman (94 reputation)SSC Journeyman (94 reputation)SSC Journeyman (94 reputation)SSC Journeyman (94 reputation)SSC Journeyman (94 reputation)

Group: General Forum Members
Points: 94 Visits: 804
Trying to answer the same question this morning found this page http://reportingservicestnt.blogspot.co.uk/2012/05/sql-server-failed-jobs-in-ssms.html

Extracted what l wanted from the code, and this query l think answers the question
SELECT  JH.run_date ,
JH.run_time ,
CAST(CAST(JH.run_date AS VARCHAR) + ' '
+ LEFT(RIGHT('000000' + CAST(JH.run_time AS VARCHAR), 6), 2) + ':'
+ SUBSTRING(RIGHT('000000' + CAST(JH.run_time AS VARCHAR), 6), 3, 2)
+ ':' + RIGHT(RIGHT('000000' + CAST(JH.run_time AS VARCHAR), 6), 2) AS SMALLDATETIME) AS [DateTime] ,

CAST(LEFT(RIGHT('000000' + CAST(JH.run_time AS VARCHAR), 6), 2) + ':'
+ SUBSTRING(RIGHT('000000' + CAST(JH.run_time AS VARCHAR), 6), 3, 2)
+ ':' + RIGHT(RIGHT('000000' + CAST(JH.run_time AS VARCHAR), 6), 2) AS TIME) AS [Time]
FROM msdb.dbo.sysjobhistory AS JH



A little late with a reply maybe help someone else.

Or could also use the undocumented function here http://www.mssqltips.com/sqlservertip/2850/querying-sql-server-agent-job-history-data/
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)

Group: General Forum Members
Points: 97984 Visits: 38995
Instead of all the conversions you could use the scalar function msdb.dbo.agent_datetime.

For example:



declare @rundate int, @runtime int;

set @rundate = 20140502;
set @runtime = 512;

select msdb.dbo.agent_datetime(@rundate,@runtime);




Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
dwilliscp
dwilliscp
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2215 Visits: 784
Hey thanks for the help.
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)

Group: General Forum Members
Points: 221600 Visits: 42003
Lynn Pettis (5/2/2014)
Instead of all the conversions you could use the scalar function msdb.dbo.agent_datetime.

For example:



declare @rundate int, @runtime int;

set @rundate = 20140502;
set @runtime = 512;

select msdb.dbo.agent_datetime(@rundate,@runtime);





While that's convenient, if you ever need performance out of it, I don't believe you'll get it. Here's the code from that function. It's hard for me to believe they do conversions to NCHAR in this numeric-only process...


CREATE FUNCTION agent_datetime(@date int, @time int)
RETURNS DATETIME
AS
BEGIN
RETURN
(
CONVERT(DATETIME,
CONVERT(NVARCHAR(4),@date / 10000) + N'-' +
CONVERT(NVARCHAR(2),(@date % 10000)/100) + N'-' +
CONVERT(NVARCHAR(2),@date % 100) + N' ' +
CONVERT(NVARCHAR(2),@time / 10000) + N':' +
CONVERT(NVARCHAR(2),(@time % 10000)/100) + N':' +
CONVERT(NVARCHAR(2),@time % 100),
120)
)
END



--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search