Monday, March 19, 2012

calculate and return % in Stored Procedure

This is my SP:

SELECT
CAST(id AS varchar(10)) + ' - ' + UserName AS 'User List',
Scanned,
Scripts AS 'Total Scripts',
Processed,
CAST((Processed/ Scripts)* 100.0 as int) AS 'DONE (%)'
FROM tblworkQueue


Processed and Scripts are both DataTypes of int
My DONE(%) column comes back with 0

Any idea where I am going wrong?

thats because you are CASTing it as INT, if the value is < 1 it will be 0. Try to cast it as decimal(10,2).|||

It is could be also if both your fields you use for dividing you useCAST((Processed/ Scripts)* 100.0 as int) processed and Scripts are integer values and I assume that processed is smaller so when you divide integers result is also integer so you get something below 1 and it is rounded to 0. try to use

CAST((100.00 *Processed/ Scripts) as int)

maybe it will work better?

No comments:

Post a Comment