Can you help me with Divided by zero Error - sql

Select ‘contribution amount/rate’= isnull(CAST(priceAmountMax/priceAmountpercetage as Decimal (15,3)),0)
From PriceList
I am sharing a part of my query. I’m using SQL Server. According to my query result I get:
divide by zero error encountered.
How can I edit this query. Thanks for your help.

Put NULLIF around priceAmountpercetage, so that the division is against null instead of zero:
isnull(CAST(priceAmountMax / nullif(priceAmountpercetage, 0) as Decimal (15,3)),0)

You can convert the result to NULL when divider is 0 as follows:
Select case when priceAmountpercetage <> 0
then isnull(CAST(priceAmountMax/priceAmountpercetage as Decimal (15,3)),0)
end
From PriceList

Related

SQL Decimal points to be remove extra values.?

I have some case about 8.89291 value in SQL database.
In this case if I'm running:
select cast(ROUND(8.89291,0) as float)
The output is : 9
What I'm actually looking for is
The output is : 8.9
How can I get this value?
Will you guys please help me?
Your precision is 0 so its giving the whole value.
select cast(round(8.89291, 1) as float)
If you are open to casting, then you don't also need to call ROUND. Consider casting to NUMERIC(10,1):
SELECT CAST(8.89291 AS NUMERIC(10,1));
Demo
(demo for SQL Server, but the above would run on many other databases as well)
You had selected 0 that mean float function will treat as Integer ,for the float you have to put 1 instead of 0 ,please check below one.
select cast(ROUND(8.89291,1) as float)
You can use
SELECT CAST(ROUND(8.89291,2) AS NUMERIC(12,1))
or
SELECT CAST(8.89291 AS NUMERIC(12,1))

how to get only 2 digits after decimal point?

SQL Server 2012
My output is 24.242553, but I need only 2 digits after my decimal point.
Here is my query :
AVG(cast(dbo.vw_NEW_UG.SCORE_A20 as decimal))AS ACT_SUPER_SCORE
I also tried this, but its not working.
AVG(cast(dbo.vw_NEW_UG.SCORE_A20 as decimal(10,2)))AS ACT_SUPER_SCORE
Thanks.
You can use ROUND function like this
CAST( ROUND(AVG(cast(dbo.vw_NEW_UG.SCORE_A20 as decimal) ) , 2 ) AS decimal(18,2))
Move your cast to the outside of your average function and try that:
cast(AVG(dbo.vw_NEW_UG.SCORE_A20) as decimal)AS ACT_SUPER_SCORE

Division in SQL Server

I am trying to use division in SQL Server. I have tried the code snippet below.
SELECT m.[Profit] / f.[Target]
And I have also tried
SELECT SUM(m.[Profit] / f.[Target])
but I get the error
Divide by zero error encountered.
Warning: Null value is eliminated by an aggregate or other SET operation.
What is the reason for this and how can I fix it? Suggested fixes online say that this code should work but it doesn't.
Thanks
Use NULLIF to avoid divided by zero exception
SELECT SUM(m.[Profit] / NULLIF(f.[Target],0))
When denominator is zero then it will be replaced with NULL
If I were trying to determine the % of a goal (inferred by profit /target columns), I would try something like:
SELECT
CASE WHEN ISNULL(f.[Target],0) = 0 THEN NULL -- or whatever you need to return when the target value is undefined, or it is 0
ELSE m.[Profit] / f.[Target] END AS [result]
The error you get specifies exactly what is the problem. Your code is trying to divide by zero, which is mathematically impossible.
Please NULLIF to avoid this error.

SQL: Unable to CAST a query

SELECT CAST ((SUM(r.SalesVolume)/1000) AS decimal(3,3)) FROM RawData r
The above is a part of a query that I am trying to run but returns an error:
Lookup Error - SQL Server Database Error: Arithmetic overflow error converting int to data type numeric.
Not sure what this means.
The result column looks like(Without dividing by 1000 and casting):
Total_Sales_Volume
64146
69814
68259
56318
66585
51158
44365
49855
49553
88998
102739
55713
Tried casting as float but doesnt help.
The Problem is decimal(3,3) --> this means a number with 3 digit, 3 of them behind the decimal point. If you want a number like this 1234567.123 you would have do declare it as decimal(10,3)
Try this:
SELECT CAST ((SUM(r.SalesVolume)/1000.0) AS decimal(6,3)) FROM RawData r
decimal(3,3) means that you allow numbers with 3 digits in total, and 3 of these are behind the comma ... I think you meant decimal(6,3)
EDIT: In addition, you need to to divide by 1000.0, not by 1000.
If you divide by 1000, it is an integer division.
If you divide by 1000.0, then it becomes a decimal division, with commas.
Try following:
SELECT CAST ((SUM(r.SalesVolume)/1000) AS numeric(6,3)) FROM RawData r

Why do I keep getting 0 when dividing

I have the following piece of code that always results in 0.00. I know for sure that the weight_score is an actual number. Can someone help me format it so that it results in a decimal number. Using SQL server 2008 and this code is in a view. Thanks a lot!
CAST(SUM(weight_score * (45/100)) as decimal(10,2)) As avg_score
45/100 is implicitly treated as an INT divided by another INT, which results in an INT that gets rounded to 0. Add a decimal to each to work around that.
CAST(SUM(weight_score * (45.0/100.0)) as decimal(10,2)) As avg_score
45/100 is treated as INT.So change it to 45.0/100.0.
select CAST(SUM(weight_score * (45.0/100.0)) as decimal(10,2)) As avg_score
Even if you write the following code you will get 0
select (45/100)
If you want to avoid the error, you can use the NULLIF function.