Why do I keep getting 0 when dividing - sql

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.

Related

SQL update shows successfully updated message but does not update the value [duplicate]

In Microsoft SQL Server 2005, why do the following commands produce integer results?
SELECT cast(151/6 AS DECIMAL(9,2))
SELECT 151/6
In the first you are getting the result of two integers and then casting the result as DECIMAL(9,2). In the second you're just dividing two integers and that's expected.
If you cast one of the integers as a decimal BEFORE you do the division, you'll get a decimal result.
SELECT 151/CAST(6 AS DECIMAL (9,2))
Yes that is standard behavior
do
SELECT 151/6.0
or
SELECT 151/(CONVERT(DECIMAL(9,2),6))
or
SELECT 151/(6 * 1.0)
Because 151 and 6 are integers and you are doing integer division, even before the cast.
You need to make sure at least one of the arguments is a float type:
SELECT 151.0/6
Or
SELECT 151/6.0
Not a direct answer to your question. Still worth to take a look at Operators in Expressions if you need this in SSRS
/ Divides two numbers and returns a floating-point result.
\ Divides two numbers and returns an integer result.
Mod Returns the integer remainder of a division.
You need to give a placeholder for decimal places as well
Example
SELECT 151.000000/6
OR
SELECT 151/6.000000
Both will produce
25.16666666
For the same reason they would in C#, Java and other mainstream languages.
In integer arithmetic, the CAST is after the maths...
The CAST statement is a bit verbose. You can use the following instead:
DECLARE #TO_FLOAT FLOAT = 1.0;
SELECT (1 * #TO_FLOAT) / 2;
Or use a different multiplier type like DECIMAL if you prefer.
Try this:
SELECT 1.0*cast(151/6 AS DECIMAL(9,2))
SELECT 1.0*151/6

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 find values with certain number of decimal places using SQL?

I'm trying to figure out a way, using SQL, to query for values that go out to, say, 5 or more decimal places. In other words, I want to see only results that have 5+ decimal places (e.g. 45.324754) - the numbers before the decimal are irrelevant, however, I still need to see the full number. Is this possible? Any help if appreciated.
Assuming your DBMS supports FLOOR and your datatype conversion model supports this multiplication, you can do this:
SELECT *
FROM Table
WHERE FLOOR(Num*100000)!=Num*100000
This has the advantage of not requiring a conversion to a string datatype.
On SQL Server, you can specify:
SELECT *
FROM Table
WHERE Value <> ROUND(Value,4,1);
For an ANSI method, you can use:
SELECT *
FROM Table
WHERE Value <> CAST(Value*100000.0 AS INT) / 100000.0;
Although this method might cause an overflow if you're working with large numbers.
I imagine most DBMSs have a round function
SELECT *
FROM YourTable
WHERE YourCol <> ROUND(YourCol,4)
This worked for me in SQL Server:
SELECT *
FROM YourTable
WHERE YourValue LIKE '%._____%';
select val
from tablename
where length(substr(val,instr(val, '.')+1)) > 5
This is a way to do it in oracle using substr and instr
You can use below decode statement to identify maximum decimal present in database table
SELECT max(decode(INSTR(val,'.'), 0, 0, LENGTH(SUBSTR(val,INSTR(val,'.')+1)))) max_decimal
FROM tablename A;

Decimals missing in Division Output

I'm a rookie SQL Programmer, but have searched here and many other SQL Forums and can't figure out why my simple division script is still ignoring the decimals. I've CAST EVERYTHING as Decimal, but still don't get any in the output . .
(CAST((ABS(CAST(CAST(SUM(h4qqnb) AS DECIMAL(12,4)) - CAST(SUM(h4hdnb) AS DECIMAL(12,4))AS DECIMAL(12,4)))/CAST(SUM(h4hdnb) AS DECIMAL(12,4))) AS DECIMAL(10,2))*100)||'%' AS Count_Variance_Rate,
What am I missing?
thanks!
That's a seriously ugly expression, with far too much CASTing, but essentially what you're doing (just looking at the outermost CAST statement) is saying
CAST(someNumber as DECIMAL(10,2)
which is going to give you a number with two decimal places of precision. You're then multiplying that by 100, which is going to give you an integer.
If you're trying to get a percentage value formatted to two decimal places, you can do it like this, assuming that h4qqnb and h4hdnb are decimal fields to begin with:
concat(cast(cast(abs(sum(h4qqnb) - sum(h4hdnb)) / sum(h4hdnb) as decimal(10, 4)) * 100 as decimal(10, 2)), '%') as Count_Variance_Rate2
Working example at http://sqlfiddle.com/#!2/279752/5/0

Integer division in sql server

In Microsoft SQL Server 2005, why do the following commands produce integer results?
SELECT cast(151/6 AS DECIMAL(9,2))
SELECT 151/6
In the first you are getting the result of two integers and then casting the result as DECIMAL(9,2). In the second you're just dividing two integers and that's expected.
If you cast one of the integers as a decimal BEFORE you do the division, you'll get a decimal result.
SELECT 151/CAST(6 AS DECIMAL (9,2))
Yes that is standard behavior
do
SELECT 151/6.0
or
SELECT 151/(CONVERT(DECIMAL(9,2),6))
or
SELECT 151/(6 * 1.0)
Because 151 and 6 are integers and you are doing integer division, even before the cast.
You need to make sure at least one of the arguments is a float type:
SELECT 151.0/6
Or
SELECT 151/6.0
Not a direct answer to your question. Still worth to take a look at Operators in Expressions if you need this in SSRS
/ Divides two numbers and returns a floating-point result.
\ Divides two numbers and returns an integer result.
Mod Returns the integer remainder of a division.
You need to give a placeholder for decimal places as well
Example
SELECT 151.000000/6
OR
SELECT 151/6.000000
Both will produce
25.16666666
For the same reason they would in C#, Java and other mainstream languages.
In integer arithmetic, the CAST is after the maths...
The CAST statement is a bit verbose. You can use the following instead:
DECLARE #TO_FLOAT FLOAT = 1.0;
SELECT (1 * #TO_FLOAT) / 2;
Or use a different multiplier type like DECIMAL if you prefer.
Try this:
SELECT 1.0*cast(151/6 AS DECIMAL(9,2))
SELECT 1.0*151/6