I want to divide two integers: 8/15 and return "53.33".
I've tried every single combination, and I've found that this is the only way I can return the desired value:
select cast(100*cast(8/cast(15 as decimal(10,4)) as decimal(18,4)) as decimal(18,2))
Is there a shorter way?
Thanks.
You are doing interger division. Use float, and multiply by 100.
select 8/15.00 * 100
Or, for your precision, just do the cast once
select cast(8/15.00 * 100 as decimal (10,2))
If these are integers in a table then multiple by 1.0 and cast once
DEMO
declare #table table (int_one int, int_two int)
insert into #table
values
(8,15)
select
cast(((int_one * 1.0) / int_two) * 100 as decimal(10,2))
from #table
try this:
select (cast (8 as money)/cast(15 as money) * 100)
Related
Convert number from 002541500(Last 4 digits are decimal places) to 254.1500 in sql server
This is simple maths:
CONVERT(int,Yourcolumn) / 10000.00
If you have to have 4 decimal places, use CONVERT:
CONVERT(decimal(10,4), CONVERT(int,YourColumn) / 10000.00)
declare #i varchar(50) = '00251500'
--Option 1:
select convert(decimal(9,4), CONVERT(int, #i) / 10000.00)
By multiplying the varchar with 1, the basetype gets converted to numeric. By multiplying this result with .0001 the scale changes from 0 to 4.
SELECT 1 * '902541501' * .0001
Result
90254.1501
SQL_VARIANT_PROPERTY:
SELECT
SQL_VARIANT_PROPERTY(1 * '902541501' * .0001, 'BaseType') BaseType,
SQL_VARIANT_PROPERTY(1 * '902541501' * .0001, 'Scale') Scale
Result
BaseType Scale
numeric 4
I found the way to fix the issue
declare #imp_ori varchar(50) = '00251500'
select
cast(left(#imp_ori,4) + '.' + right(#imp_ori,4) as numeric(10,4)) as StringBuildCast
result: 25.1500
How can I convert (money/int)*100 to a float datatype?
a.ResidualValue is of type money, while ListPriceCar is of type int.
The following outputs a number with a comma as the decimal separator, so I presume it is still a money field, and not a float as I wish:
CONVERT(float, CONVERT(float, REPLACE(CONVERT(nvarchar(10), a.ResidualValue), ',', '.'))/ListPriceCar)*100 AS 'Residual value %'
Result: 62,825
What is issue with simple cast as below:
DECLARE #money AS money
SET #money = $2345234.35
SELECT #money, CAST(#money AS float)
What's the value of ResidualValue? If ResidualValue contains a comma, then comma is your decimal char. You shouldn't convert it into a point then. You can divide a money value by an int value.
declare #a money = 10.5;
declare #b int = 20;
select (#a / #b) * 100
select cast((#a / #b) * 100 as float)
In your example, the result was already given as an float. You can test this by running the following example code.
--INPUT VARIABLES
declare #ResidualValue money = 5000.54
declare #ListPriceCar int = 15000
--OUTPUT VARIABLE
declare #ResultCalculation as sql_variant;
--CALCULATION
set #ResultCalculation = CONVERT(float, CONVERT(float, REPLACE(CONVERT(nvarchar(10), #ResidualValue), ',', '.'))/#ListPriceCar)*100
--GET INFORMATION ON THE OUTPUT VARIABLE
select sql_variant_property(#ResultCalculation,'BaseType') AS 'Base Type',
sql_variant_property(#ResultCalculation,'Precision') AS 'Precision',
sql_variant_property(#ResultCalculation,'Scale') AS 'Scale',
sql_variant_property(#ResultCalculation,'TotalBytes') AS 'TotalBytes',
sql_variant_property(#ResultCalculation,'Collation') AS 'Collation',
sql_variant_property(#ResultCalculation,'MaxLength') AS 'MaxLength';
There is an easier way to calculate your answer:
CAST(#ResidualValue/#ListPriceCar * 100 as float)
I have a situation like this
I got a column with 'money' type, 2 decimal . Example data:(65.00)
I need to add 12 zero / 000000000000 to it so that the output would be like this:
(65.00 convert to 6500) + (000000000000) = 000000006500
Output: 000000006500
How can I achieve this?. Thank you for your help and suggestion
You can do this with a couple of casts, multiplying by 100, and using REPLICATE('0') to pad with the requisite number of zeroes).
I'm assuming you DO want up to 2 x trailing decimals, but no more.
DECLARE #value MONEY;
SET #value = 65.123;
DECLARE #intValue BIGINT;
SET #intValue = CAST(#value * 100.0 AS BIGINT);
SELECT REPLICATE('0',12-LEN(#intValue)) + CAST(#intValue AS NVARCHAR(20));
Returns 000000006512
If you need to do this on a set, a CTE can be used for the intermediate step, e.g.
WITH cte AS
(
SELECT CAST(MoneyField * 100.0 AS BIGINT) AS intValue
FROM SomeTable
)
SELECT
REPLICATE('0',12-LEN(cte.intValue)) + CAST(cte.intValue AS NVARCHAR(20))
FROM cte;
Fiddle here
It is Possible .But output Column should be in the type of varchar(15) .If you want to do further operation of your output you have to convert that into int or whatever
SELECT CONCAT(REPEAT('0',12-LENGTH(65.00)),(65.00*100));
I am working with a table with a column 'value' with the type varchar(100).
All values in that column must be changed by multiplying them with 0.001 but my following update script fails due to "arithmetical overflow error while converting varchar to a numeric type".
update testTable
set value = cast ((value * 0.001) as varchar);
I must not change the type of the column and it holds values between 0 and 4294966796.
How do i cast correctly to get the calculation in the update working?
I tried cast (cast ((value * 0.001)) as float) as varchar) but it still throws the error.
CAST(CAST( value AS NUMERIC) *0.001 AS VARCHAR(100))
Here try this :
update testTable
set value = cast ((cast(value as float) * 0.001) as varchar);
If it still fails then one of the rows have non-numeric value
You can;
update testTable cast(cast(value as decimal) * 0.001 as varchar(32))
One way
update testTable
set value = convert(float,value) * 0.001
A simple example you can run
DECLARE #z varchar(100)
SELECT #z = CONVERT(float,'123') * 0.001
SELECT #z
0.123
If your values exceed the size of a float, then you can do the arithmetic as strings. This is a special case, because multiplying by 0.001 is just moving the decimal place over three places to the left. The following works for values greater than 1000, with or without decimal places:
update testTable
set value = (case when charindex('.', value) = 0
then left(value, len(value) - 3)+'.'+right(value, 3)
else left(value, charidnex('.', value) - 3) + '.' +
replace(right(value, len(value) - charindex('.', value) + 4), '.', '')
end)
If you have values less than 100, then you will need to prepend the values with 0s for this to work.
In SQL, I have col1 and col2. Both are integers.
I want to do like:
select col1/col2 from tbl1
I get the result 1 where col1=3 and col2=2
The result I want is 1.1
I put round(col1/col2,2). The result is still 1.
I put decimal(col1/col2,2). The decimal is not built in function.
How can I do exactly to get 1.1?
Just another approach:
SELECT col1 * 1.0 / col2 FROM tbl1
Multiplying by 1.0 turns an integer into a float numeric(13,1) and so works like a typecast, but most probably it is slower than that.
A slightly shorter variation suggested by Aleksandr Fedorenko in a comment:
SELECT col1 * 1. / col2 FROM tbl1
The effect would be basically the same. The only difference is that the multiplication result in this case would be numeric(12,0).
Principal advantage: less wordy than other approaches.
You will need to cast or convert the values to decimal before division. Take a look at this
http://msdn.microsoft.com/en-us/library/aa226054.aspx
For example
DECLARE #num1 int = 3 DECLARE #num2 int = 2
SELECT #num1/#num2
SELECT #num1/CONVERT(decimal(4,2), #num2)
The first SELECT will result in what you're seeing while the second SELECT will have the correct answer 1.500000
SELECT CAST (col1 as float) / col2 FROM tbl1
One cast should work. ("Less is more.")
From Books Online:
Returns the data type of the argument with the higher precedence. For more information about data type precedence, see Data Type Precedence (Transact-SQL).
If an integer dividend is divided by an integer divisor, the result is an integer that has any fractional part of the result truncated
CAST( ROUND(columnA *1.00 / columnB, 2) AS FLOAT)
There may be other ways to get your desired result.
Declare #a int
Declare #b int
SET #a = 3
SET #b=2
SELECT cast((cast(#a as float)/ cast(#b as float)) as float)
just convert denominator to decimal before division e.g
select col1 / CONVERT(decimal(4,2), col2) from tbl1