Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Working with SQL Agent Durations


Working with SQL Agent Durations

Author
Message
intrope
intrope
SSC Rookie
SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)

Group: General Forum Members
Points: 25 Visits: 93
You can actually avoid using trunctation. Consider that if @t is the sql agent time, @t % 100 will be seconds, @t % 10000 - @t % 100 will be minutes * 100, and @t - @t % 10000 will be hours * 10000. So, the subsequent division by 100 or 10000 is already an integer value.


DECLARE @t int = 1234556; --4546 --5; --56; --1234556;

SELECT
   @t AS AgentDuration,
   (@t - @t % 10000) /10000 AS hours,
   (@t % 10000 - @t % 100) /100 AS minutes,   
   @t % 100 AS SECONDS,

   ((@t - @t % 10000) /10000) * 60 * 60
   + ((@t % 10000 - @t % 100) /100) * 60
   + @t % 100 AS totalseconds



Added bonus: it'll work for negative values too. Not that output will mean anything (GIGO!)
Cyclone
Cyclone
SSC Rookie
SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)SSC Rookie (48 reputation)

Group: General Forum Members
Points: 48 Visits: 244
Yo Dude!

Great post. Thanks for sharing!
:-)
Carolyn Sellers
Carolyn Sellers
SSC Veteran
SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)SSC Veteran (245 reputation)

Group: General Forum Members
Points: 245 Visits: 1488
Dear All,

If running script in SQL 2005 or greater, you can save yourself some time by using msdb system function agent_datetime. Check out this thread http://www.sqlservercentral.com/Forums/Topic542581-145-2.aspx.

Give this script a try;

select [step_name]
,run_duration
,run_date
,msdb.dbo.agent_datetime(run_date,run_time) as StartDateTime
,dateadd(s,datediff(s,msdb.dbo.agent_datetime(run_date,0),msdb.dbo.agent_datetime(run_date,run_duration))
,msdb.dbo.agent_datetime(run_date,run_time)) as EndDateTime
from msdb.dbo.sysjobhistory
where datediff(hh, msdb.dbo.agent_datetime(run_date,run_time), getdate()) <= 240
order by StartDateTime desc;

Hehe
Kyle Neier ,
Kyle Neier ,
SSC Veteran
SSC Veteran (202 reputation)SSC Veteran (202 reputation)SSC Veteran (202 reputation)SSC Veteran (202 reputation)SSC Veteran (202 reputation)SSC Veteran (202 reputation)SSC Veteran (202 reputation)SSC Veteran (202 reputation)

Group: General Forum Members
Points: 202 Visits: 1188
It seems that my article about Powershell has sparked some T-SQL discussion, so I decided to take the prompt and run some performance analysis on the different solutions presented here.

You can read the detail over on my blog (http://sqldbamusings.blogspot.com/2012/08/t-sql-performance-of-sql-agent-duration.html), but essentially, the conversions that use math instead of string manipulation seem to be faster and cover a wider range of potential values.

Before I ran the tests, I really had no clue what the results would be, which is why I wanted to run them. Thanks to all of you the great suggestions - another testament to how great the SQL community is!
Rich Mechaber
Rich Mechaber
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1087 Visits: 3661
Kyle Neier , (8/24/2012)
It seems that my article about Powershell has sparked some T-SQL discussion, so I decided to take the prompt and run some performance analysis on the different solutions presented here.

You can read the detail over on my blog (http://sqldbamusings.blogspot.com/2012/08/t-sql-performance-of-sql-agent-duration.html), but essentially, the conversions that use math instead of string manipulation seem to be faster and cover a wider range of potential values.

Before I ran the tests, I really had no clue what the results would be, which is why I wanted to run them. Thanks to all of you the great suggestions - another testament to how great the SQL community is!

Well, that is interesting.

Carolyn, I wasn't aware of that msdb sys function, so thanks for pointing it out.

That said, I was pretty surprised to find that my hack string manipulation performed 7X faster than the msdb function agent_datetime. Thanks for testing it!

I suppose if I were running a lot of code against a very large table of job histories, I might go back and tweak my code. I suspect that most DBAs running code against msdb.dbo.sysjobhistory are doing it for similar reasons though: a once-a-day (or thereabouts) system check and reporting. Still, I prefer your approach, both for performance and pedagogically: why deal with times as strings?

Kyle, have you seen any documentation anywhere as to why MS has coded times this way?

Thanks
Rich
Thanks for
OldFashionGang
OldFashionGang
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 388
+5 cent's of mine
select top 100
name
,
convert(datetime,(convert(varchar(8),h.run_date)))
+(
+substring (right( '000000'+(convert (varchar (6), h.run_time) ),6),1,2)
+':'+substring (right( '000000'+(convert (varchar (6), h.run_time) ),6),3,2)
+':'+substring (right( '000000'+(convert (varchar (6), h.run_time) ),6),5,2)
)
begining
,
convert (varchar ,convert(int,reverse (substring (reverse (convert (varchar , run_duration)),5,4))))+
+':'+substring (right( '000000'+(convert (varchar , run_duration) ),6),3,2)
+':'+substring (right( '000000'+(convert (varchar , run_duration) ),6),5,2)
duration
,
-- EdsTime as begining+duration
convert( datetime, convert(varchar(8),h.run_date))
+ (
substring (right( '000000'+(convert (varchar (6), h.run_time) ),6),1,2)
+':'+substring (right( '000000'+(convert (varchar (6), h.run_time) ),6),3,2)
+':'+substring (right( '000000'+(convert (varchar (6), h.run_time) ),6),5,2)
)
+

( -- add hours
dateadd (hh, convert(int,reverse (substring (reverse (convert (varchar , run_duration)),5,4))
), 0)
+
-- add mins
dateadd (mi, convert (int,
substring (right( '000000'+(convert (varchar , run_duration) ),6),3,2)
), 0)
+
-- add seconds
dateadd (ss, convert (int,
substring (right( '000000'+(convert (varchar , run_duration) ),6),5,2)
), 0)
)
endtime
from msdb..sysjobhistory h with( nolock), msdb..sysjobs j with( nolock)
where h.step_id=0 and h.job_id=j.job_id order by instance_id desc


works for several years to make this:

Neha05
Neha05
SSC-Addicted
SSC-Addicted (466 reputation)SSC-Addicted (466 reputation)SSC-Addicted (466 reputation)SSC-Addicted (466 reputation)SSC-Addicted (466 reputation)SSC-Addicted (466 reputation)SSC-Addicted (466 reputation)SSC-Addicted (466 reputation)

Group: General Forum Members
Points: 466 Visits: 60
Good article.
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