January 28, 2010 at 5:09 am
Hi All,
How can I remove trailing zeros in decimals? For instance if the value is 12.4086000 to just return 12.4086, 7.2300 to return 7.23 etc.
I could write a function for this but I want to find out if there's a pre-defined function in 2005 that I can use to avoid reinventing the wheel.
Thanks!
January 28, 2010 at 5:37 am
You need to write function. There is no such predefined function for your requirement.
January 28, 2010 at 6:46 am
Can you be more specific as to what are you trying to achieve i.e. what will you do with the result?
I suggest you first check if you cannot simply use the formatting possibilities of your front-end tool (Excel, Reporting Services etc.)
If that is not an option, maybe you can convert the decimal to a float (beware of rounding issues!) or convert to a string e.g.
DECLARE @a DECIMAL(10,5)
SET @a = 12.4086000
SELECT @a, REPLACE(RTRIM(REPLACE(@a, '0',' ')),' ','0')
SET @a = 7.23
SELECT @a, REPLACE(RTRIM(REPLACE(@a, '0',' ')),' ','0')
Beware that the latter still leaves you with the decimal point if your number is an integer. But I think I've shown you the path on how to deal with that too
January 28, 2010 at 8:56 am
Thanks a lot for the lead.
I suppose this can take care of the trailing decimal point for integers:
SELECT REPLACE(REPLACE(RTRIM(REPLACE(@a, '0' ,' ')), ' ','0') + ' ', '. ', '')
I'm just using a SELECT statement to pull up some measurements (from a field with 6 decimal place precision) and dont want to clog the result with trailing zeroes.
Once again, thanks!
Viewing 4 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy