build a query for having jobs status and duration : name|run_status|run_duration


I try to build a query for having name|run_status|run_duration :

set nocount on; select distinct REPLACE(REPLACE(REPLACE(,'[',''),']',''),' ', '')  +'|'+ '' + h.run_duration + '' +  '|' +  case h.run_status when 0 then 'Failed' when 1 then 'Successful' when 3 then 'Cancelled' when 4 then 'In Progress' end as JobStatus from msdb..sysjobhistory h, msdb..sysjobs j where j.job_id = h.job_id and h.run_date = (select max(hi.run_date) from msdb..sysjobhistory hi where h.job_id = hi.job_id and hi.run_date is not null) and h.run_status <> '4' and instance_id = (select max(hi.instance_id) from msdb.dbo.sysjobhistory hi where h.job_id = hi.job_id) order by 1

The following error message is returned :
Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the varchar value '|' to data type int.

How can I resolve it ?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You need to convert your h.run_duration to varchar:
select distinct 
    REPLACE(REPLACE(REPLACE(,'[',''),']',''),' ', '')  +'|'+ '' + 
    CAST(h.run_duration AS VARCHAR(30))+ '' +  '|' +  
    case h.run_status when 0 then 'Failed' when 1 then 'Successful' 
          when 3 then 'Cancelled' when 4 then 'In Progress' end as JobStatus 

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.