Truncation using round function isn't achieved as expected in sql server - sql

I have a field stored in float datatype. I need to convert it to numeric without it getting implicitly rounded in the process of conversion.
I have tried round(float_data,scale,1). Seems to work fine for most of the cases.but when the number of digits after decimal places is less than scale mentioned in round function it tries to floor down the number rather than appending 0 at the end.
For instance, round (0.0243,5,1) returns 0.02429. Why isn't it simply truncating the number to the number of digits mentioned?
I know this issue is when we use float as source datatype but I cannot change the source datatype.
The same truncation happens right when the same is achieved via ssis. Is there any way in sql to achieve this?

Because when converted to a float, the decimal 0.0243 is stored as 0.02429999969899654388427734375, which truncates to 0.02429. Looks like you want to round instead of truncate, eg
declare #f float = 0.0243
select round(#f,5,0)

Related

SQL Server: Convert FLOAT to NVARCHAR, maximum accuracy, without scientific notation

Without using scientific notation, I need to convert a FLOAT to a string, without showing scientific notation, and capturing all possible precision.
For example, when I execute SELECT 1E0 / 1346E0 I get the following result:
This is how SQL Server displays a FLOAT value by default.
In this case, it displays 18 decimal places, which is more than the STR function can provide.
It also does not add any trailing zeros.
If SQL Server Management Studio can do this, can I also get this conversion in my code?
I need to avoid scientific notation at all costs, even if there are 20 leading zeros after the decimal point. A long string is not a problem.
Unfortunately, the CONVERT function does not provide what I need, even with style 3
try format()
SELECT
1E0 / 1346E0
, format(1E0 / 1346E0,'N18')
declare #float float = 0.000742942050520059
select cast(cast(#Float as decimal(38,35)) as varchar(200))
As was also noted, format works too, although I'm not a huge fan of it as it's a kind of heavy hitting CLR. but for one offs, it's fine.

converting float to varchar sql

Im writing a stored procedure that will convert float columns to varchar as part of its process when returning the data.
i dont want to convert everything to varchar(max) bec i think its probably more efficient not to. what is the largest size varchar i need to use -
convert(NVARCHAR(????), floatcolumn)
100?
i want to make sure i never get a result that looks like 8397Xe10
Presumably, you are using SQL Server (based on the code in your question). If you don't want exponential notation, then use the str() function (documented here). The length of the string doesn't have a large impact on performance, but you can do something like:
select str(floatcolumn, 100, 8) -- or whatever you think reasonable bounds are for your data

SQL ROUND() function with truncate takes 119.1 and returns 119.09

I have data for pounds and pence stored within concatenated strings (unfortunately no way around this) but can not guarantee 2 decimal places.
E.g. I may get a value of 119.109, so this must translated to 2 decimal places with truncation, i.e. 119.10, NOT 119.11.
For this reason I am avoiding "CAST as Decimal" because I do not want to round. Instead I am using ROUND(amount, 2, 1) to force truncation at 2 decimal places.
This works for the most part but sometimes exhibits strange behaviour. For example, 119.10 outputs as 119.09. This can be replicated as:
ROUND(CAST('119.10' AS varchar),2,1)
My target field is Decimal(19,4) (but the 3rd and 4th decimal places will always be 0, it is a finance system so always pounds and pence...).
I assume the problem is something to do with ROUNDing a varchar....but I don't know anyway around this without having to CAST and therefore introduce rounding that way?
What is happening here?
Any ideas greatly appreciated.
This is due to the way floating point numbers work, and the fact that your string number is implicitly converted to a floating point number before being rounded. In your test case:
ROUND(CAST('119.10' AS varchar),2,1)
You are implicitly converting 119.10 to float so that it can be passed to the ROUND function, 119.10 exactly cannot be stored as a float, proven by running the following:
SELECT CAST(CONVERT(FLOAT, '119.10') AS DECIMAL(30, 20))
Which returns:
119.09999999999999000000
Therefore, when you round this with truncate you get 119.09.
For what it is worth, you should always specify a length when converting to, or declaring a varchar

Prevent Rounding in SQL With Decimal Types

I am working on an exercise that requires me to call a stored procedure within another procedure. The procedure being called simply divides two numbers, however the result seems to be rounded upwards.
In the exercise, 5.0 is divided by 10.0, and the result is 1. The result should obviously be 0.5. The goal is to make the output display correctly without just changing the DECIMAL type.
Here is the code:
GO
CREATE PROCEDURE uspDivide2Numbers
#decValue1 AS DECIMAL
,#decValue2 AS DECIMAL
,#decResult AS DECIMAL OUTPUT
AS
SET NOCOUNT ON
SELECT #decResult = #decValue1 / #decValue2
GO
CREATE PROCEDURE uspCallAnotherStoredProcedure
AS
SET NOCOUNT ON
DECLARE #decResult AS DECIMAL
EXECUTE uspDivide2Numbers 5.0, 10.0, #decResult OUTPUT
SELECT #decResult AS decResult
GO
uspCallAnotherStoredProcedure
Any help would be appreciated, I don't really understand why decimal values round in the first place, so it's probably an issue on my end.
The default scale of a decimal is zero, which means that what you have is actually an integer type. So, it's not possible to return the value 0.5 without changing the data type.
If you specify a precision and scale for the type, like decimal(18,2), it can handle numbers that are not integers. The scale is how many digits will be stored after the decimal separator.
Depending on your needs, you might also consider the float and real floating point data types.

sql rounding value

I have been successful at rounding my value to one decimal point, but I would like to trim off the multitude of trailing zeros. For example, this:
ROUND(SUM(Duration),1)
...ends up giving me:
16.9000000000000000
how do I trim all those trailing zeros.
mssql
The round function rounds the number, but it leaves the data type unchanged:
ROUND(3.14,1) --> 3.10
By casting a number to numeric(x,1), you both round it and change it's data type to single digit precision:
CAST(3.14 as numeric(6,1)) --> 3.1
For your query, such a cast could look like:
select cast(sum(duration) as numeric(6,1))
But the eventual display format depends on the client. SQL Server Management Studio will display numeric(x,1) with one digit behind the dot, but it is free to display it in another way. Formatting a number is best done client side, outside of SQL.
Use:
CONVERT(Decimal(9, 1), ROUND(SUM(duration), 1))
The second parameter of the DECIMAL data type is the precision - 1 will give you a single digit after the decimal point.