3

I have a scenario where I need to round and then remove the extra zeros from numbers. So if I have a number that I have rounded (12.456400000) I want the zeros to be removed. Is there a function I can use to remove those numbers? The round function appears to leave the zeros in place?

As always greatly appreciate the input.

--S

Denis Valeev
  • 5,975
  • 35
  • 41
scarpacci
  • 8,957
  • 16
  • 79
  • 144
  • duplicate question: http://stackoverflow.com/questions/2938296/remove-trailing-zeros-from-decimal-in-sql-server – JeffO Sep 22 '10 at 16:42

3 Answers3

1

Sure, try this:

select replace(rtrim(replace('12.456400000', '0', ' ')), ' ', '0')
Denis Valeev
  • 5,975
  • 35
  • 41
1

Create a User Defined Function (MSDN link) and minimize code.:

CREATE FUNCTION [dbo].[RemoveTrailingZeros] 
(
    @Value decimal
)
RETURNS decimal
AS
BEGIN
    RETURN replace(rtrim(replace(@Value, '0', ' ')), ' ', '0')
END

You would use it as follows:

SELECT dbo.RemoveTrailingZeros(12.456400000)
NakedBrunch
  • 48,713
  • 13
  • 73
  • 98
0

Presumably you know how many decimal places you are rounding to, so you can cast to a decimal data type with the correct number of DP outside the ROUND

CAST(ROUND(Field, 4) AS DECIMAL(12, 4))
JamWheel
  • 300
  • 2
  • 7