Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Divide and return negative result

Author  Topic 

michaelb
Yak Posting Veteran

69 Posts

Posted - 2009-01-19 : 16:42:32
Hi,

I need to divide price by gross profit in a query to return the gross profit %, but where the price is 0, I want it to return a negative result.

I had problems with the divide by zero error and found the following overcomes this:
isnull(t1.price / NULLIF(t1.grossbuypr, 0),0)*100

However this reports negatives as 0. Is there any way to make it report as a negative value?

Thanks,
Michael

revdnrdy
Posting Yak Master

220 Posts

Posted - 2009-01-19 : 16:54:12
Hello;

Here is one potential way.. Note I did not have a chance to test it.
It will return a negative 1 if either Price or grossbuypr is 0.
Otherwise it will perform your calculation..

SELECT ProductNumber, Name, Price =
CASE
WHEN Price = 0 THEN '-1'
WHEN grossbuypr = 0 THEN '-1'
ELSE (t1.price / grossbuypr)*100
END
FROM YourDatabaseTable

that should help you out a bit.. There may of course be other solutions. Also beware of integer math ; )


r&r

Go to Top of Page
   

- Advertisement -