Sample data:
1) 0.02500 = 25
2) 1.0000=100
I tried below code but it's converting 0.25 to 30
Select Try_cast(workcountry as decimal (10,2)*100
Multiply first, then cast. This should give you your an expected behavior.
SELECT TRY_CAST(0.02500*100 AS decimal(10,2))
Your problem is the rounding, 0.025 with 2 decimal places rounds to 0.03. You then multiply that by 100 to get 3. Add an extra decimal place and you'll be fine
select try_cast(0.02500 as decimal (10,3)) * 100
You can try this:
declare #workcountry float = 0.025;
select try_cast(#workcountry * 100. as decimal (10,2))
Related
When I run this simple operation in SQL server:
Select 800.0 /30.0
I get the value 26.666666, where even if it rounds for 6 digits it should be 26.666667.
How can I get the calculation to be accurate? I tried to search about it online and I found a solution where I cast each operand to a high precision decimal before the operation, but this will not be convenient for me because I have many long complex calculations. think there must be a better solution.
When a using division, in SQL Server, any digits after the resulting scale are truncated, not rounded. For your expression you have a decimal(4,1) and a decimal(3,1), which results in a decimal(10,6):
Precision = p1 - s1 + s2 + max(6, s1 + p2 + 1)
Scale = max(6, s1 + p2 + 1)
As a result, 26.66666666666666~ is truncated to 26.666666.
You can get around this by can increasing the size of the precision and scale, and then CONVERT back to your required precision and scale. For example, increase the precision and scale of the decimal(3,1) to decimal(5,2) and convert back to a decimal(10,6):
SELECT CONVERT(decimal(10,6),800.0 / CONVERT(decimal(5,3),30.0));
This returns 26.666667.
This might helpful:
Use ROUND (Transact-SQL)
SELECT ROUND(800.0 /30.0, 5) AS RoundValue;
Result:
RoundValue
26.666670
I believe it's because SQL Server takes your numbers as decimal values (which are exact e.g., 6.6666 and 6.6667 means exactly those values, not 6 and two-thirds) rather than float values (which can work with approximate numbers).
If you explicity cast/convert it to a float at the start, you should get your calculations running smoothly.
Here's some examples to demonstrate the difference between int, decimal, and float calculations
Dividing 20 by 3
Dividing 20 by 3, then multiplying by 3 again (which mathematically should be 20).
SELECT (20/3) AS int_calc,
(20/3) * 3 AS int_calc_x3,
(CAST(20 AS decimal(10,3)) /3) AS dec_calc,
(CAST(20 AS decimal(10,3)) /3) * 3 AS dec_calc_x3,
(CAST(20 AS float) /3) AS float_calc,
(CAST(20 AS float) /3) * 3 AS float_calc_x3
with the following results
int_calc int_calc_x3 dec_calc dec_calc_x3 float_calc float_calc_x3
6 18 6.666666 19.999998 6.66666666666667 20
In your case, you can use
Select CAST(800.0 AS float) /30.0
which results in 26.6666666666667
Note if you then multiply back by 30, it gets the correct result e.g.,
Select (CAST(800.0 AS float) /30.0) * 30
results in 800. Solutions dealing with decimals will not have this.
Note also that once you have it as a float, then it should stay a float until converted back to a decimal or an int somehow (e.g., saved in a table as an int). So...
SELECT A.Num / 30
FROM (Select ((CAST(800.0 AS float) /30.0) * 30) AS Num) AS A
will still result in 26.6666666666667
This will hopefully help you in your long complex calculations.
Basically I am trying to calculate the progress of the current quarter, represented as a percentage. Currently I have:
(DATEPART(dd,#AsOf)/91) * 100
We are using 91 days as a fixed solution for the quarter. They necessity for 100% accuracy is not required.
#AsOf is being passed in as a DATETIME type.
I have tried multiple ways and I receive 0. I assume it is because I am using INT instead of DECIMAL but I tried that and I still get 0.
You should just be able to force it to be a decimal by adding a decimal point, like 91.0 and 100.0 to avoid integer division issues:
DECLARE #date DATETIME
set #date = getdate();
select DATEPART(dd,#date) TheDay,
(DATEPART(dd,#date)/91.0) DivBy91,
(DATEPART(dd,#date)/91.0) * 100.0 Result
Result:
TheDay DivBy91 Result
19 0.208791 20,8791000
If an integer dividend is divided by an integer divisor, the result is an integer that has any fractional part of the result truncated.
What integer division will do is produce 0 in column 2, which is what is causing your result to be 0.
I am trying to calculate a rate by using two variables from a subquery and i want my result to have three values after the decimal.
Variables:
calls -- type(integer)
boxes -- type(float)
ROUND((calls / boxes) * 100.0, 3) Call_Rate
only returns two decimal points
when i use
(CAST(calls AS DECIMAL(10,3)) / (boxes AS DECIMAL(10,3)) * 100.0 Call_Rate
this code returns three digits after the decimal but the third digit is always 0, so it is being rounded.
When converting both variables to float, it also returns just 2 values after decimal.
Is there any way to perform this operation to have it return 3 decimal points.
(CAST(calls AS DECIMAL(10,5)) / (boxes AS DECIMAL(10,5)) * 100.0 Call_Rate
When you multiply by 100, you are shifting the decimal point to the right two places, so you need two more places in your decimal conversion.
The 1st calculation actually rounds to three digits, but you probably run this query from SQL Assistant and there's a default precision of two. You can change it in Tools > Options > Number of decimal places to display for Float columns
The result of the 2nd calculation is based on Teradata's calculation rules for decimals, see DECIMAL Result Data Type
In short: Multiply first, then divide
100 * CAST(calls AS DECIMAL(10,3)) / CAST(boxes AS DECIMAL(10,3)) AS Call_Rate
Or keep the Float and do a final cast:
cast( 100 * calls / boxes as decimal(10,3))
I have a number with two digits after the decimal point; I try to cast this number using CAST for example:
Select SUM(CAST((123345.56) as decimal(28, 2))) * 100 AS AMOUNT
I get the following result
12334556.00
but I want get only 12334556 without displaying two digits after the decimal point.
Thank you.
It is not clear what you really want to do, but
SELECT SUM(CAST((123345.56 * 100.0) AS DECIMAL(28,0))) AS AMOUNT
gives 12334556.
When you use a number in the format 1234.56 it is a decimal literal in SQL Server: see "decimal constants" in Constants (Transact-SQL).
Integer data type is whole numbers, so cast your calculated value as int.
Select SUM(CAST((123345.56 * 100) as int)) AS AMOUNT
Using T-SQL and Microsoft SQL Server I would like to specify the number of decimal digits when I do a division between 2 integer numbers like:
select 1/3
That currently returns 0. I would like it to return 0,33.
Something like:
select round(1/3, -2)
But that doesn't work. How can I achieve the desired result?
The suggestions from stb and xiowl are fine if you're looking for a constant. If you need to use existing fields or parameters which are integers, you can cast them to be floats first:
SELECT CAST(1 AS float) / CAST(3 AS float)
or
SELECT CAST(MyIntField1 AS float) / CAST(MyIntField2 AS float)
Because SQL Server performs integer division. Try this:
select 1 * 1.0 / 3
This is helpful when you pass integers as params.
select x * 1.0 / y
It's not necessary to cast both of them. Result datatype for a division is always the one with the higher data type precedence. Thus the solution must be:
SELECT CAST(1 AS float) / 3
or
SELECT 1 / CAST(3 AS float)
use
select 1/3.0
This will do the job.
I understand that CASTing to FLOAT is not allowed in MySQL and will raise an error when you attempt to CAST(1 AS float) as stated at MySQL dev.
The workaround to this is a simple one. Just do
(1 + 0.0)
Then use ROUND to achieve a specific number of decimal places like
ROUND((1+0.0)/(2+0.0), 3)
The above SQL divides 1 by 2 and returns a float to 3 decimal places, as in it would be 0.500.
One can CAST to the following types: binary, char, date, datetime, decimal, json, nchar, signed, time, and unsigned.
Looks like this trick works in SQL Server and is shorter (based in previous answers)
SELECT 1.0*MyInt1/MyInt2
Or:
SELECT (1.0*MyInt1)/MyInt2
Use this
select cast((1*1.00)/3 AS DECIMAL(16,2)) as Result
Here in this sql first convert to float or multiply by 1.00 .Which output will be a float number.Here i consider 2 decimal places. You can choose what you need.
If you came here (just like me) to find the solution for integer value, here is the answer:
CAST(9/2 AS UNSIGNED)
returns 5
I was surprised to see select 0.7/0.9 returning 0.8 in Teradata given they're already as floats/decimal numbers! I had to do cast(0.7 as float) to get the output that I was after.
When using literals, the best way is to "tell" SQL
which type you mean.
if you want a decimal result, add decimal point ".0" to your numbers:
SELECT 1.0 / 3.0
Result
0.333333
if you want a float (real) result, add "e0" to your numbers:
SELECT 1e0 / 3e0
Result
0.333333333333333