SSMS - Conversion failed when converting the varchar value to data type int

Hi Experts,

I have this:
CONVERT(varchar(8), DATEADD(MILLISECOND, SUM(TimeProcessing), 0), 8)/Count(Name) as TimeTook

Open in new window

But am getting error:
Conversion failed when converting the varchar value '03:18:20' to data type int

Please help and thanks
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

Pawan KumarDatabase ExpertCommented:
Please try this-

Why are your dividing date  by some INTEGER Value? It is not possible.

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
Pawan KumarDatabase ExpertCommented:
edited my last comment.
Amour22015Author Commented:
Hi Experts,

This query is for finding the amount of times a report has been used.
SELECT COUNT(Name) AS ExecutionCount
	  ,SUM(TimeDataRetrieval) AS TimeDataRetrievalSum
	  ,SUM(TimeProcessing) AS TimeProcessingSum
	  ,SUM(TimeRendering) AS TimeRenderingSum
	  ,SUM(ByteCount) AS ByteCountSum
	  ,SUM([RowsCount]) AS RowCountSum
	  ,CONVERT(varchar(8), DATEADD(MILLISECOND, SUM(TimeProcessing), 0), 8) as [Time]
	  ,CONVERT(varchar(8), DATEADD(MILLISECOND, SUM(TimeProcessing), 0), 8)/Count(Name) as TimeTook

Open in new window

So ExecutionCount is how many times a report has been executed
Time shows how much time it takes for a report to render
So I need the amount of times Executed divided by the time it took...

So like:
If report executed
30074 times
3:18:20 to render
3:18:20 / 30074 = Would give amount of time it took per each time report ran...

Please help and thanks
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

Amour22015Author Commented:
Great thanks
Nakul VachhrajaniTechnical Architect, Capgemini IndiaCommented:
It's doable. A time value is ultimately the number of seconds passed from a given instant. Hence, it's ultimately a number and hence it is possible to divide the TIME with an integer value.

Here's an example (using the test data provided in your example above):

USE tempdb;

DECLARE @timeDifference TIME;
DECLARE @executionCount INT = 30074;

SELECT @timeDifference = '3:18:20';

SELECT @timeDifference AS TimeDifference,
       DATEDIFF(SECOND,'1900-01-01 00:00:00.000',CAST(@timeDifference AS DATETIME) ) AS TotalExecutionTimeInSeconds, 
       DATEDIFF(SECOND,'1900-01-01 00:00:00.000',CAST(@timeDifference AS DATETIME) )/(@executionCount * 1.0) AS TimePerExecution;
TimeDifference   TotalExecutionTimeInSeconds TimePerExecution
---------------- --------------------------- ---------------------------------------
03:18:20.0000000 11900                       0.39569062977987

Open in new window

Pawan KumarDatabase ExpertCommented:
Well If you want to get to seconds the we can use below also to get the results.

DECLARE @timeDifference TIME = '3:18:20'
DECLARE @executionCount INT = 30074
SELECT Times,Seconds,Seconds*1./30074 ExecTimes FROM
	select @timeDifference Times
	SELECT SUM(LEFT(@timeDifference,2) * 3600 + SUBSTRING(CAST(@timeDifference AS VARCHAR(40)), 4,2) * 60 + SUBSTRING(CAST(@timeDifference AS VARCHAR(40)), 7,2)) 

Open in new window


Times            Seconds     ExecTimes
---------------- ----------- ---------------------------------------
03:18:20.0000000 11900       0.395690

(1 row(s) affected)

Open in new window

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
Query Syntax

From novice to tech pro — start learning today.