This question already has answers here:
How to get a float result by dividing two integer values using T-SQL?
(10 answers)
Decimal values in SQL for dividing results
(6 answers)
Closed 6 years ago.
I'm using MSSQL 2014 and I'd like to know if there is a better way to display 2 decimals in a simple division like 10/3.
SELECT 10/3 -- returns 3
SELECT CONVERT(DECIMAL(10, 2), 10/3) -- RETURNS 3
SELECT CAST(10/3 AS DECIMAL(10,2)) -- RETURNS 3
The only way I found to make it work is by casting the divisor as float:
SELECT 10/CAST(3 AS FLOAT) -- returns 3.333333...
SELECT CONVERT(DECIMAL(10, 2), 10/CAST(3 AS FLOAT)) -- RETURNS 3.33
SELECT CAST(10/CAST(3 AS FLOAT) AS DECIMAL(10,2)) -- RETURNS 3.33
Are these two last options the best approach available?. Is it possible to do this without any cast/conversion?
Yeah, to truncate you have to specify that you only want two digits. If it were me I'd do:
SELECT cast(10/3.0 as decimal(10,2))
I get a float if I do,
select 10/3.0
https://stackoverflow.com/a/11719098/28045
Division of one integer by another will result in an integer. Either cast the numbers or use variables of float or decimal type having values of 10 and 3.
Related
This question already has answers here:
Integer division in sql server
(8 answers)
Closed 10 months ago.
I declared a variable #avgMis as a decimal(2,2) in SQL Server. Then I tried to divide two integers, expecting a decimal value. Instead my answer is 0.00.
How do I divide #avgMis = (1/(1+2+1)) to get 0.25 in SQL Server?
cast as decimal
DECLARE #x INT = 10, #y INT = 3, #z decimal(6,2)
SELECT #z = CAST(#x AS decimal(8,4))/CAST(#y AS DECIMAL(8,4))
SELECT #z
result would be 3.33
in your situation
SELECT CAST( CAST(1 AS DECIMAL(6,2))/CAST((1+2+1) AS DECIMAL(6,2)) AS DECIMAL(6,2)) AS result
Say if I need to round multiple numbers to 2 digits now, but I don't want to repeat using round(..., 2) or format(...).
Is there any method to set up the float numbers with 2 digits globally?
select cast(float_column as decimal(10,2))
from your_table
Declare the column or variable of type numeric(18, 2)
You can also use with the CONVERT function
select CONVERT(numeric(18, 2) , 5.54722)
This question already has an answer here:
How to lower bound the value 6.15 to 6.1 in SQL Server?
(1 answer)
Closed 4 years ago.
How to round off the below values in SQL?
35.38
53.08
6.15
5.38
to
35.4
53.1
6.1
5.4
SELECT ROUND(colname - 0.01, 1) AS RoundValue FROM TableName;
SYNTAX OF ROUND():
ROUND ( numeric_expression , length [ ,function ] )
Demo
http://sqlfiddle.com/#!18/2f9d9/11
Have you tried round()?
select round(val, 1)
However, I would recomend converting to a decimal format:
select cast(val as decimal(10, 1))
The cast() not only changes the value, but it also changes the type. That means that tools should also print out only one decimal place.
How do I code format the return data in 2 decimals and with percentage format like 100.00% or 67.39% instead of 100.000000 or 67.391304?
SUM(qa.scripting1+qa.conduct1+qa.conduct2+qa.conduct3)*100.0/46 as 'C%'
I tried ROUND() but I got the error stating that the round function requires 2 to 3 arguments?
ROUND(SUM(qa.scripting1+qa.conduct1+qa.conduct2+qa.conduct3)*100.0/46) as 'C%'
Thanks!
You can convert to a decimal your original value:
CONVERT(VARCHAR(20), CONVERT(DECIMAL(18,2), SUM(qa.scripting1+qa.conduct1+qa.conduct2+qa.conduct3)*100.0/46) ) + '%' as 'C%'
The first number in the decimal represents the number of digits in the number including decimal places, and the second number represents the number of decimal places.
You should pass number of decimals in second parameter to round function. For formating you can cast number to money and then cast to varchar:
select cast(cast(ROUND(SUM(123.12321)*100.0/46, 2) as money) as varchar) + '%'
Using Round and Cast will work. First round to 2 decimal places then convert to a decimal with 2 places to truncate the excess zeros.
select cast(Round(yourValue, 2) as decimal(18,2))
Sql Fiddle
You can use Format function
select FORMAT(100.0000, 'N' , 'en-us')
returns 100.00
and
select FORMAT(67.391304, 'N' , 'en-us')
returns 67.39
EDIT
In version below 2012 you can do this
SELECT CAST(67.391304 AS NUMERIC(10, 2))
returns 67.39
You can just do:
select FORMAT(0.391304, '##0.00%')
But keep in mind that it implicitly multiplies by 100, so the above will display as 39.13%.
This question already has answers here:
Integer division in sql server
(8 answers)
Closed 8 years ago.
When I use a DateDiff in a SELECT statement in SQL Server, I get an int value back:
select DATEDIFF(day,'01/01/2011','15/05/2011')
from Table1
Result is : 134
select DATEDIFF(day,'01/01/2011','15/05/2011')/15
from Table1
Result is : 8
select cast(DATEDIFF(day,'01/01/2011','15/05/2011') / 15 as Decimal(9,2))
from Table1
Result is : 8.00
But in reality the result is : 8.9333333
How can I change the result from int to Double?
I want to get the result 8.9333333 - how can I do this?
The problem is that you're using CAST after dividing. If you perform the division after the CAST, you'll get the answer you want:
select
cast(DATEDIFF(day,'01/01/2011','15/05/2011') as Decimal(9,2))/15
from Table1
You need to cast your inputs into the division as decimal first:
select cast(DATEDIFF(day,'2011-01-01','2011-05-15') as Decimal(9,2))/15.0
Otherwise you are dividing an integer by an integer - you'll get an integer out. If you then cast this to a decimal after the division, you are going to get 8.0.
SELECT CAST(DATEDIFF(day,'01/01/2011','15/05/2011') AS Decimal(9,2))/15
FROM Table1