Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12345»»»

Script to calculate when a job ended. Expand / Collapse
Author
Message
Posted Monday, March 11, 2013 8:56 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 9:46 PM
Points: 21,187, Visits: 14,880
Jeff Moden (3/11/2013)
David Burrows (3/11/2013)
SELECT DISTINCT j.name as job_name, 
msdb.dbo.agent_datetime(run_date, run_time) AS [run_datetime],
DATEADD(second,DATEDIFF(second,0,msdb.dbo.agent_datetime(19000101, run_duration)),msdb.dbo.agent_datetime(run_date, run_time)) AS [run_endtime]
FROM msdb..sysjobhistory h
INNER JOIN msdb..sysjobs j ON h.job_id = j.job_id
WHERE j.name NOT LIKE 'Backup%'
AND j.name NOT LIKE 'Cleanup%'
AND j.name NOT LIKE 'Shrink%'
AND j.name <> 'WMI_Disk_Space_Notification'
AND j.name <> 'syspolicy_purge_history'
ORDER BY run_endtime DESC

Note that this uses an undocumented system function


Thank you Mr. Burrows. I learned something new. In the past, I've written just such a function that apparently works the same way. I don't need to do that anymore.


Very nice indeed. I learned something new as well.




Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Posting Data Etiquette - Jeff Moden
Hidden RBAR - Jeff Moden
VLFs and the Tran Log - Kimberly Tripp
Post #1429318
Posted Monday, March 11, 2013 9:00 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: 2 days ago @ 3:00 PM
Points: 4,175, Visits: 4,257
SQLRNNR (3/11/2013)
Jeff Moden (3/11/2013)
David Burrows (3/11/2013)
SELECT DISTINCT j.name as job_name, 
msdb.dbo.agent_datetime(run_date, run_time) AS [run_datetime],
DATEADD(second,DATEDIFF(second,0,msdb.dbo.agent_datetime(19000101, run_duration)),msdb.dbo.agent_datetime(run_date, run_time)) AS [run_endtime]
FROM msdb..sysjobhistory h
INNER JOIN msdb..sysjobs j ON h.job_id = j.job_id
WHERE j.name NOT LIKE 'Backup%'
AND j.name NOT LIKE 'Cleanup%'
AND j.name NOT LIKE 'Shrink%'
AND j.name <> 'WMI_Disk_Space_Notification'
AND j.name <> 'syspolicy_purge_history'
ORDER BY run_endtime DESC


Note that this uses an undocumented system function


Thank you Mr. Burrows. I learned something new. In the past, I've written just such a function that apparently works the same way. I don't need to do that anymore.


Very nice indeed. I learned something new as well.


Very nice. I tried adding the formatted duration but I can't quite get it.

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/

Post #1429323
Posted Monday, March 11, 2013 9:09 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 9:46 PM
Points: 21,187, Visits: 14,880
Welsh Corgi (3/11/2013)
SQLRNNR (3/11/2013)
Jeff Moden (3/11/2013)
David Burrows (3/11/2013)
SELECT DISTINCT j.name as job_name, 
msdb.dbo.agent_datetime(run_date, run_time) AS [run_datetime],
DATEADD(second,DATEDIFF(second,0,msdb.dbo.agent_datetime(19000101, run_duration)),msdb.dbo.agent_datetime(run_date, run_time)) AS [run_endtime]
FROM msdb..sysjobhistory h
INNER JOIN msdb..sysjobs j ON h.job_id = j.job_id
WHERE j.name NOT LIKE 'Backup%'
AND j.name NOT LIKE 'Cleanup%'
AND j.name NOT LIKE 'Shrink%'
AND j.name <> 'WMI_Disk_Space_Notification'
AND j.name <> 'syspolicy_purge_history'
ORDER BY run_endtime DESC


Note that this uses an undocumented system function


Thank you Mr. Burrows. I learned something new. In the past, I've written just such a function that apparently works the same way. I don't need to do that anymore.


Very nice indeed. I learned something new as well.


Very nice. I tried adding the formatted duration but I can't quite get it.

Thank you.


What are you getting?




Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Posting Data Etiquette - Jeff Moden
Hidden RBAR - Jeff Moden
VLFs and the Tran Log - Kimberly Tripp
Post #1429330
Posted Monday, March 11, 2013 9:24 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: 2 days ago @ 3:00 PM
Points: 4,175, Visits: 4,257
I have the following code:

SELECT DISTINCT j.name as job_name, run_duration,
msdb.dbo.agent_datetime(run_date, run_time) AS [run_datetime],
DATEADD(second,DATEDIFF(second,0,msdb.dbo.agent_datetime(19000101, run_duration)),msdb.dbo.agent_datetime(run_date, run_time)) AS [run_endtime]
,SUBSTRING(run_duration, 1, 2) + ':' + SUBSTRING(run_duration, 3, 2) + ':' +
SUBSTRING(run_duration, 5, 2) AS run_duration
FROM msdb..sysjobhistory h
INNER JOIN msdb..sysjobs j ON h.job_id = j.job_id
WHERE j.name = 'WMI_Disk_Space_Notification'
ORDER BY run_endtime DESC



I get the error listed below:
Msg 8116, Level 16, State 1, Line 1
Argument data type int is invalid for argument 1 of substring function.



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/

Post #1429345
Posted Monday, March 11, 2013 9:38 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 2:35 AM
Points: 7,045, Visits: 6,785
You have to cast run_duration to varchar first.

Try

STUFF(STUFF(RIGHT('000000'+CAST(run_duration as varchar(6)),6),5,0,':'),3,0,':')




Far away is close at hand in the images of elsewhere.

Anon.

Post #1429351
Posted Sunday, March 31, 2013 9:43 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: 2 days ago @ 3:00 PM
Points: 4,175, Visits: 4,257
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.


SELECT j.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
FROM dbo.sysjobs j
JOIN dbo.sysjobsteps js
ON js.job_id = j.job_id
JOIN master.dbo.sysservers s
ON s.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/

Post #1437294
Posted Sunday, March 31, 2013 12:23 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 7:35 PM
Points: 22,992, Visits: 31,471
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.


SELECT j.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
FROM dbo.sysjobs j
JOIN dbo.sysjobsteps js
ON js.job_id = j.job_id
JOIN master.dbo.sysservers s
ON s.srvid = j.originating_server_id
WHERE j.name = 'Any Job Name'


Any help would be greatly appreciated.

Thanks.


So, what is the problem?



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)
Post #1437306
Posted Sunday, March 31, 2013 12:42 PM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: 2 days ago @ 3:00 PM
Points: 4,175, Visits: 4,257
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/

Post #1437311
Posted Sunday, March 31, 2013 12:53 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 7:35 PM
Points: 22,992, Visits: 31,471
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

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)
Post #1437314
Posted Sunday, March 31, 2013 1:00 PM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: 2 days ago @ 3:00 PM
Points: 4,175, Visits: 4,257
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/

Post #1437316
« Prev Topic | Next Topic »

Add to briefcase ««12345»»»

Permissions Expand / Collapse