November 18, 2015 at 10:59 pm
Hi,
How can you covert two integer columns into date and time then concatenate I to single column.
In job history table, I want to show run_date and run_time into a single column. And show the run_ time as more readable clearly 20hr 11 min..
Thanks
November 19, 2015 at 2:16 am
How I have got around the problem in the past.
;with cte as
(select
convert(varchar,run_date) as rundate,
right('000000'+convert(varchar,run_time),6) as runtime
from msdb.dbo.sysjobhistory
)
select convert(datetime,rundate+' '+left(runtime,2)+':'+left(right(runtime,4),2)+':'+right(runtime,2))
from cte
November 19, 2015 at 2:19 am
November 19, 2015 at 2:21 am
Robert vd Berg (11/19/2015)
Is this what your looking for?SELECTdbo.agent_datetime(run_date, run_time)
FROMsysjobhistory;
+1, learned something new with that, didn't even know that function existed.
November 19, 2015 at 3:01 am
November 19, 2015 at 7:32 am
I know this might seem like more work, but it should be faster than using the scalar function, which is basically a udf.
SELECT DATETIMEFROMPARTS(
run_date / 10000, --Year
(run_date % 10000)/100, --Month
run_date % 100, --Day
run_time / 10000, --Hours
(run_time % 10000)/100, --Minutes
run_time % 100, --Seconds
0) --Milliseconds
FROM msdb.dbo.sysjobhistory;
Of course, you could create your own inline tabl-valued function.
CREATE FUNCTION iagent_datetime(@date int, @time int)
RETURNS TABLE WITH SCHEMABINDING
AS
RETURN
SELECT DATETIMEFROMPARTS(
@date / 10000,
(@date % 10000)/100,
@date % 100,
@time / 10000,
(@time % 10000)/100,
@time % 100,
0) agent_real_datetime
GO
And yes, the original code for dbo.agent_datetime is inefficient.
November 19, 2015 at 9:13 am
Robert vd Berg (11/19/2015)
Is this what your looking for?SELECTdbo.agent_datetime(run_date, run_time)
FROMsysjobhistory;
You beat me to it. It's not the epitome of efficiency but it's usually good enough. Luis' "iSF" would work much faster for large volumes.
--Jeff Moden
Change is inevitable... Change for the better is not.
November 19, 2015 at 9:42 am
Hi,
Thanks for the reply.
I use
dateadd(hour, (run_time / 1000000) % 100,
dateadd(minute, (run_time / 10000) % 100,
dateadd(second, (run_time / 100) % 100,
dateadd(millisecond, (run_time % 100) * 10, cast('00:00:00' as time(2))))))
from sysjobhistory
The result is coming something
00:14:49:29
00:23:00:00
00:09:25:00
But it is not correct. It has to come like this
14:49:29:00
23:00:00:00
09:25:00:00
November 19, 2015 at 9:45 am
I need same format as Anthony provided but without CTE because I already have several sub queries in my main query
November 19, 2015 at 9:58 am
You have the wrong formulas to calculate each part. Check my previous post for correct formulas.
November 19, 2015 at 10:48 am
Thank you.
November 19, 2015 at 10:50 am
Cotto vs Canelo Live Stream Tv Telecast
https://www.facebook.com/Miguel-Cotto-vs-Canelo-Alvarez-Live-Stream-PPV-Boxing-1503111929983691/
Cotto vs Canelo Live Stream[/url]
Viewing 12 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply