how to get only 2 digits after decimal point? - sql

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

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

Can you help me with Divided by zero Error

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

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))

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.