Need to figure out how to parse GETDATE() to compare it against HHMMSS value in a table to find elapsed time
I need to find a way to create an alert that will alert me of the elapsed time of a non-sql batch job. The information on the batch job is recorded in the database.
I have a table named F986110 that records the submission time of these batch jobs. The date is recorded in the JCSBMDATE field as a six number value 1YYDDD, where YY = the last two digits of the current year and DDD is the current day numbered from Jan 1. For example, Feb 25, 2011 would be 111056. The time is recorded in the JCSBMTIME column in a military style hhmmss format, but n开发者_运维知识库o leading 0 for times before 10am. There is also a status column that indicates processing (P), waiting (W), and done (D).
My task is to send an alert out for all jobs that are in P or W status for longer than 15 minutes. I've been banging my head against this for some time. I've found a lot of good stuff that is pointing me in the right direction but having to parse the system time, compare it to each record returned, and alerting on those (if any) that match is beyond my skills.
Convert JCSBMDATE (1YYDDD) and JCSBMTIME to datetime
declare @t table (JCSBMDATE char(6),JCSBMTIME varchar(6))
insert @t select '111056', '104602'
insert @t select '199365', '81602'
select dateadd(d,
right(JCSBMDATE,3)-1,
convert(datetime,'01/01/' + substring(JCSBMDATE,2,2),3)) +
convert(datetime, stuff(
stuff(JCSBMTIME,
len(JCSBMTIME)-3,0,':'),
len(JCSBMTIME),0,':'))
from @t
Reverse conversion which is more index friendly
select '1' +
right(year(getdate()),2) +
right(1000+datepart(dy, getdate()),3) as JCSBMDATE,
right(1+replace(convert(char(8),getdate(),8),':',''),6) as JCSBMTIME
You can easily adjust getdate()
with dateadd(mi,-15,getdate())
to get JCSBMDATE and JCSBMTIME values 15 minutes ago.
精彩评论