November 14, 2007 at 3:16 am
hi,
I want to create a query with a formula to compute a percentage.
The query(illustrative not actual) is like
Select income, expenses from sales
What i want is to have a column which contains ((income-espenses)/income)*100 i.e a percentage value in the same query using the other columns.
I tried using
select income, expense, ((income-expense)/income)*100 as percentageexpense from sales
but this is not working.
Can someone help?
November 14, 2007 at 3:36 am
What kind of data type is income? And expense?
If they are decimal data type, your query works.
November 14, 2007 at 10:18 pm
What error are you receiving?
K. Brian Kelley
@kbriankelley
November 19, 2007 at 10:34 am
And Divide By Zero errors can kill your query if you don't use Try..Catch or some other test for income being zero, so make sure to take that into account when coding.
Also, if you're just looking to throw a percent sign at the end of the result, cast/convert the formula as a String and concatenate the percent sign.
Convert(char(15),((Income-Expense)/Income) * 100) + '%'
November 19, 2007 at 12:02 pm
If income and expense are integers, it won't work unless you force a conversion as in the following...
select income, expense, ((income-expense)/income)*100.0 as percentageexpense from sales
--Jeff Moden
Change is inevitable... Change for the better is not.
November 19, 2007 at 12:45 pm
Jeff, you have to force the conversion to numeric before the division operation.
This code shows that with a check for divide by zero also.
select
income,
expense,
[Percent] = ((income-expense+0.00)/nullif(income,0))*100.0
from
(
-- Test Data
select income =4, expense =3 union all
select income =0, expense =3
) a
Results:
income expense Percent
----------- ----------- -------------------------------
4 3 25.00000000000000
0 3 NULL
(2 row(s) affected)
November 21, 2007 at 6:37 am
Hi
Select
Income,
Expense,
(
(Cast(Income as Float) - Cast(Expense as Float)) / Cast( ISNULL(NULLIF(Income,0),1) as Float)
) * 100 As percentageexpense
FROM
(
select income =4, expense =3
union all
select income =0, expense =3
) a
Bhavani.
November 21, 2007 at 7:24 am
Bhavani Taninki (11/21/2007)
HiSelect
Income,
Expense,
(
(Cast(Income as Float) - Cast(Expense as Float)) / Cast( ISNULL(NULLIF(Income,0),1) as Float)
) * 100 As percentageexpense
FROM
(
select income =4, expense =3
union all
select income =0, expense =3
) a
Bhavani.
I don't see why you are doing this:
ISNULL(NULLIF(Income,0),1)
For the second row, it returns a value of -300, something that is obviously wrong. The percentage in this case just does not make sense if the income is zero, so NULL is a better answer.
November 22, 2007 at 3:35 pm
Your problem may with the the data types. SQL Server is not very friendly with divides of INT / INT.
This will return 0
DECLARE
@Expense INT
, @Income INT
SELECT @Expense = 100, @Income = 200
SELECT @Expense / @Income
This will return a proper value
SELECT CAST(@Expense AS MONEY) / @Income
November 22, 2007 at 5:11 pm
Jeff, you have to force the conversion to numeric before the division operation
Thanks, for the catch, Michael... more coffee! 😀
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply