I am using sql server 2000 and facing round function issue like the following statement working fine.
SELECT ROUND(5 * 7.83, 1)
The result will be 39.2
But when I get these values from the table, it gives 39.1, meaning it truncates and does not round up.
SELECT ROUND(rate * qty, 1)
FROM tbl
The result will be 39.1
rate and qty columns data types are float. Insert 5 in qty and 7.83 in rate, then check it. How I can fix it?
Convert the table values to real,
SELECT ROUND(convert(real,rate)*convert(real,qty),1)
Your sample simply query is not reflective of the data types involved.
Try these two instead:
SELECT ROUND(5 * 7.83, 1)
SELECT ROUND(cast(5 as float) * cast(7.83 as float), 1)
The 2nd one matches your table data types. Float datatypes are not meant for precise decimal calculations, use a decimal type for those instead.
What Every Computer Scientist Should Know About Floating-Point Arithmetic
Without losing too much precision for normal numbers, you can just cast to decimal on the fly to force human-comprehensible decimal arithmetics, e.g.
SELECT ROUND(cast(rate as decimal(10,5)) * cast(qty as decimal(10,5), 1)
FROM tbl
Related
I have about 70 million rows of data with a column that contains numbers, but it's in a float format. I need to get rid of the last 4 digits of that column i.e. I need to turn this
60871003002001
60871003002002
60871003002003
into this
6087100300
6087100300
6087100300
When I run the query
select top 3 LEFT(COLUMN, LEN(COLUMN)-4) as a from TABLE
it returns the following:
6.0871e
6.0871e
6.0871e
Does anyone know why? I'm using SQL Server. There are no nulls and each number is from 12 to 15 digits long.
Thank you!
Instead, divide by 1000 and turn into a decimal:
select cast( (col / 10000) as decimal(18, 0))
The problem you are facing is that the default conversion of a float to a string might sometimes be in scientific notation.
I have a table where one column is Price (decimal(18,9)) and another Volume (bigint).
I am multiplying both values and then applying round function but nothing works.
I want it to be 2 decimal place precision. How to do it?
SELECT
CAST((Price * Volume) AS decimal(38,2)) test1,
ROUND((Price * Volume), 2) 'VolumePrice',
CONVERT(DOUBLE PRECISION, (Price * Volume)) 'test2'
FROM a
Table values are something like this:
Price Volume
-------------------------
63.380000000 131729
63.380000000 61177
44.860000000 246475
44.860000000 246475
44.860000000 63937
97.990000000 84620
191.650000000 438821
I want to simply multiply the price by the volume, to get a total value amount.
ROUND() just changes the decimal value up or down, doesn't change the data type precision.
What you want is to convert to DECIMAL with a scale of 2.
SELECT
CONVERT(DECIMAL(18,2), Price * Volume) AS DecimalConversion
FROM
A
Converting a decimal of higher scale (Price * Volume) to a lower one will automatically round the last digit:
SELECT
CONVERT(DECIMAL(18,2), '1.901999'), -- 1.90
CONVERT(DECIMAL(18,2), '1.909999'), -- 1.91
CONVERT(DECIMAL(18,2), '1.905999'), -- 1.91
CONVERT(DECIMAL(18,2), '1.904999') -- 1.90
When an operator combines two expressions of different data types, the rules for data type precedence specify that the data type with the lower precedence is converted to the data type with the higher precedence
source: MSDN docs
In SQL Server precedence order for data types in question is :
decimal
bigint
So bigint is converted to implicitly converted to decimal.
If you need your desired results you should simply do
SELECT
VolumePrice= cast(Price * Volume as decimal(18,2) )
FROM a
See working demo
I have 2 columns which I need to divide sum(cola)/sum(ColB), but I am not getting the desired results since SQL server seems to truncate values after decimal
For eg. I have-
select 281370/1035
is giving 271 using simple division, whereas actual result of division is 271.8550724637681 and I want to display 271.8
I tried
SELECT cast(round(281370/1035,1) as numeric(36,1))
but that results 271.0
In SQL Server, you have to cast the integers to decimal and you could use Round to get desired precision.
SELECT cast(Round(CAST(281370 AS decimal) / CAST(1035 AS decimal),1,1) as decimal(10,1))
The problem is that you given the int number and want a decimal result
try this
select convert(decimal(30,10),281370.0/1035.0)
or
select Round(convert(decimal(30,10),281370.0/1035.0),1,1)
#Stormcloak gives the answer to specifically wanting a single position as a mantissa, however to return an exact answer you could "simply" implicitly change the datatype.
select 281370.0/1035
Returns:
271.855072
In Presto DB:
select (CAST(11 as decimal(8,6))/CAST(7 as decimal(8,6))) as result
result:1.571429
decimal(xp,xs)
xp--> total number of digits(before decimal point+ after decimal
point)
xs--> number of digits after the decimal point
reference: https://prestodb.io/docs/current/functions/decimal.html
Using SQL 2014 I need to append a negative sign to a list of numeric values. The data are dollar amounts with numerous places behind the decimal point. I did convert the data to numeric(15, 2)
Here is my select statement.
SELECT '-' + convert(15,2), MONEY from TABLE
I am getting the error: Arithmetic overflow error converting varchar to data type numeric.
I tried converting to varchar as well.
select '-' + CONVERT(varchar10), (convert(numeric(15, 2), MONEY)) from
TABLE
I get the same error as above. Any ideas how to accomplish this?
How about multiplying by -1 instead? Something like this:
SELECT -1 * convert(MONEY, 15.2) from TABLE
Your syntax isn't correct. I am guessing you want something like the above.
Or subtract from 0.
SELECT -convert(MONEY, 15.2) from TABLE
why not trying Cast Function
SELECT -1 * CAST(MONEY as Numeric(15.2)) from TABLE
I need to concatenate between several fields (text and numeric) and It must be accurate. Some of the fields are originally Numeric(19,6) and I need it to be with only 2 digits after the decimal point.
I'm using the following queries and if I run no. 1 I get in the CONCAT_AMOUNT a rounded numbers like so: 38156.738156.7 and in no. 2 I get it correct - 476.47476.47.
Why is it happening and how can I solve this with minimum functions?
SELECT
38156.650000 AS AMOUNT,
CAST(38156.650000 as float),
CONCAT(cast(38156.650000 as float),
cast(38156.650000 as float)) AS CONCAT_AMOUNT
SELECT
467.47 AS AMOUNT,
CAST(467.47 as float),
CONCAT(cast(467.47 as float),
cast(467.47 as float)) AS CONCAT_AMOUNT
Okay, so SQL Server makes some assumptions when converting floating point values to strings. That shouldn't be surprising. The database cannot print out an infinite number of places after the decimal point.
So, two easy choices: convert to decimal or use str():
select concat(cast(38156.650000 as decimal(10, 2)) . . .
or
select concat(str(38156.650000, 10, 2) . . .
Note: the first version is SQL standard and should work in any database.