This question already has answers here:
Truncate (not round) decimal places in SQL Server
(21 answers)
Closed 6 years ago.
Assume i have the following decimal value:
4.584406
I need a simple quick way to truncate the decimal without rounding up, so the output would be 4.5
I'm using T-SQL (SQL Server 2005/2008).
Any help will be appreciated.
using the round function you can try this
select round(4.584406, 1, 1)
the output will be
4.5
the key is the third parameter
ROUND ( numeric_expression , length [ ,function ] )
function
Is the type of operation to perform. function must be tinyint,
smallint, or int. When function is
omitted or has a value of 0 (default),
numeric_expression is rounded. When a
value other than 0 is specified,
numeric_expression is truncated.
Related
This question already has answers here:
Integer division in sql server
(8 answers)
Closed last year.
I Exec below code in SQL Server Management Studio
SELECT CAST (9/100 AS DECIMAL(8,3))
this should return 0.090 but Return 0.000 .
Who can tell what the problem is and why it gives me this output?
The problem with your current approach is that the 9/100 ratio is computed using integer division before you make the cast to decimal. By the time that cast happens, it is already too late and you've lost the decimal component. Simply I would just do:
SELECT 9 / 100.0; -- 0.090000
So long as the numerator or denominator be a decimal, the quotient will also carry the decimal result.
This question already has answers here:
TSQL Round up decimal number
(4 answers)
Closed 2 years ago.
I need to round off a numeric value in SQL Server.
Consider this example - my value is 179.8744.
I want it to be converted to 179.88
I have tried ROUND function, but it only converts if 3rd value is greater than 5. Is there any other function?
Thanks in advance.
You could get creative with the ceiling() function. This function will always round up, but to the next integer. For 2 decimal places this can be resolved by multiplying and dividing by 100.0.
-- move decimal separator up
select 179.8744 * 100.0
-- round up to next integer
select ceiling(179.8744 * 100.0);
-- move decimal separator down (complete formula)
select ceiling(179.8744 * 100.0) / 100.0;
Fiddle
use decimal or round like below!
Examples decimal
select cast(179.8744 as decimal(10,2))
OR
select round(179.8744,2)
This question already has answers here:
Truncate (not round) decimal places in SQL Server
(21 answers)
Closed 3 years ago.
The function TRUNCATE works in MySQL but I get an error in Microsoft SQL Server : Incorrect syntax near (
Basically I have list of value under discount. (Some with lots of decimal)
I just want to keep the 2 decimal without rounding.
E.g from 110.975 to 110.97, 10.259 to 10.25. How do I go about doing that?
SELECT TRUNCATE(110.975,2)
You could use round this way
ROUND (110.975 , 2 , 1 )
ROUND ( numeric_expression , length [ ,function ] )
function Is the type of operation to perform. function must be
tinyint, smallint, or int. When function is omitted or has a value of
0 (default), numeric_expression is rounded. When a value other than 0
is specified, numeric_expression is truncated.
https://learn.microsoft.com/en-us/previous-versions/sql/sql-server-2005/ms175003(v=sql.90)?redirectedfrom=MSDN
This question already has answers here:
Why is casting from float to varchar being rounded in SQL Server?
(3 answers)
Closed 3 years ago.
The following query gives the output as 123.5
SELECT STR(123.45, 6, 1);
GO
But.. The following query gives the output as 123.3
SELECT STR(123.35, 6, 1);
GO
Why it is not giving the result as 123.4 ?
SELECT STR(123.45, 6, 1);
GO
SELECT STR(123.35, 6, 1);
GO
The following query gives the output as 123.3
SELECT STR(123.35, 6, 1);
GO
Why it is not giving the result as 123.4 ?
From the documentation for SQL Server's STR function:
[the first parameter] Is an expression of approximate numeric (float) data type with a decimal point.
From my local testing, even if I pass in a DECIMAL value, the imprecision you are seeing continues, and the input parameter still gets treated as a float.
That is:
SELECT STR(CAST(123.45 AS DECIMAL(10,2)), 6, 1)
still returns 123.5.
If you want to truncate a numerical value exactly in SQL Server, then just try casting to a DECIMAL type, e.g.
SELECT CAST(123.45 AS DECIMAL(10,1))
returns 123.4 as you would expect.
Long story short, use FORMAT instead of STR, only to format strings. There is an inconsistency here indeed.
STR is rounding half to odd which is an .... odd behavior. The more common approach is round half to even, also known as Banker's rounding. Both strategies minimize the aggregated rounding error.
That's why
SELECT STR(123.45, 6, 1),STR(123.35, 6, 1);
Returns
123.5 123.3
STR is not a rounding function though, it's a string formatting function. The strings it produces aren't meant to be added. What's more, T-SQL's ROUND function rounds away from zero and
SELECT round(123.45,1), round(123.35,1);
Produces
123.50 123.40
Oracle, PostgreSQL and MySQL's ROUND behave the same way, rounding away from zero. I suspect this is part of the standard but I haven't found a relevant link yet.
The FORMAT is far more powerful. It uses the same format strings as .NET and rounds away from zero.
SELECT FORMAT(123.45, 'n1'), FORMAT(123.35, 'n1');
Produces
123.5 123.4
This question already has answers here:
Integer division in sql server
(8 answers)
Closed 7 years ago.
If I do
select 18000*13/100000
it returns 2
when it should return 2.34
How do I make it return the correct number?
thanks
select 18000*13/100000.0
Use a float either in numerator or denominator to get a float.
Use a decimal point to avoid integer division:
select 18000.0*13/100000
You can also CAST() as DECIMAL or FLOAT a field, but I like to just use mycol *1.0