Monday, December 29, 2008

Using sql agent date and time handling

I had project try to insert batch run time into database. The batchRunDt data formation is "20070430204147", the field in database is datetime,

here is the example to show you how to using fn_agentDate2DateTime to converts datetime


declare @batchRunDt varchar(20)
select @batchRunDt = '20070430204147'
print msdb.dbo.fn_AgentDateTime2DateTime(left(@batchRunDt,8),Right(@batchRunDt,6))

The result is :
Apr 30 2007 8:41PM

print msdb.dbo.fn_AgentDate2DateTime(20060806)
The result is:

Aug 9 2006 12:00AM



you can download the function in : http://sqldev.net/sqlagent/SQLAgentDateTime.htm

No comments: