Blog Pages

SQL Server: Alternate to msdb.dbo.agent_datetime

 msdb.dbo.agent_datetime converts integer date + integer time values to a datetime value.

Sometimes you won't have permissions to run agent_datetime (For example in the admin user in AWS RDS) - so you'll need an alternative to it.


DECLARE @int_date INT, @int_time INT

SELECT @int_date=20200913, @int_time=155834


SELECT msdb.dbo.agent_datetime(@int_date,@int_time)

SELECT cast(cast(@int_date as char(8))+' '+stuff(stuff(right('000000'+convert(varchar(6),@int_time),6),3,0,':'),6,0,':') as datetime)


select  jh.run_date, jh.run_time, 

msdb.dbo.agent_datetime(jh.run_date, jh.run_time) as 'agent_datetime',

cast(cast(jh.run_date as char(8))+' '+stuff(stuff(right('000000'+convert(varchar(6),jh.run_time),6),3,0,':'),6,0,':') as datetime) as fog_last_run_time

from msdb.dbo.sysjobhistory jh

No comments:

Post a Comment