concat in sql rounding the float columns - sql

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.

Related

Using LEFT with LEN Turns Float Into Weird Format (e.g. 6.0871e)

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.

SQL Division precision

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

What is '.' in SQL Server query

I just started working on one existing project and see the query which looks something like this -
select 100. * somecolumn1 / NULLIF(somecolumn2, 0.) AS ColumnValue,
from dbo.SomeTable
I am not sure what is '.' operator in select statement. Could anyone please help me understand?
Also this is just a portion of massive SQL Server query. And if I comment this particular select statement the query runs in about 7 seconds otherwise it takes about 5 minutes to execute the query. Can this statement be optimized?
SQL Server does integer division. So, 1/2 = 0, not 0.5.
To avoid integer division, you want to use values with decimal places. The simplest way to accomplish that is to use numeric literals. I would always include a 0 after (and before) the decimal place, so the value is easier to see:
select 100.0 * somecolumn1 / NULLIF(somecolumn2, 0.0) AS ColumnValue,
from dbo.SomeTable
I often accomplish this by multiplying by 1.0. Alternative, you can use cast() or convert() on an integer column.

SQL - Convert number to decimal

I'm trying to convert a number to a decimal with two decimals places.
SELECT CONVERT(DECIMAL(10,2),12345)
The above would return 12345.00 but I'm trying to achieve 123.45
You need something like that:
SELECT CONVERT(DECIMAL(15,2),12345/100.0)
SELECT CONVERT(DECIMAL(10,2),CAST(12345 as float)/CAST(100 as float))
Correction: The premise is somewhat flawed, as the data type of a literal number without a decimal point is int, not numeric as implied by the question. In that case, you do need to convert the initial value to either numeric or decimal before dividing:
SELECT CONVERT(DECIMAL,12345)/100
or
SELECT CAST(12345 AS DECIMAL)/100
(cast is the SQL standard, so if you ever want to apply this to other databases, it would be the preferred method.)
Alternately, you can just add a decimal point to the divisor, as SQL server will return the more precise data type when doing arithmetic on heterogeneous types:
SELECT 12345/100.0
According to the documentation, the numeric data type is functionally equivalent to the decimal datatype, so there's really no reason to convert between the two. It seems that all you really want to do is divide the value you have by 100:
SELECT 12345/100

sql server round function not working well

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