Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Strip Trailing Zeros 1

Status
Not open for further replies.

lyudmila

Programmer
Oct 18, 2002
54
US
How can strip trailing zeros from one of the field in my table, data type decimal(10,0), but string has different length.
Example of data values:
14080
140800
1408000
256479900
25647990
2564799000
230880
2308800
23088000

I need to use this field as a link, but data is not consistent. People who key in data, have no rules and limitations. I need to get rid off those zeros.

Any idea how I can accomplish this task?




 
here is one way:

replace all 0's with spaces, then to a right trim, then replace all spaces with zeros

like this:

REPLACE(RTRIM(REPLACE(CAST(mycolumn AS VARCHAR), '0', ' ')) , ' ', '0')

Cheyney
 
As he only wanted to strip trailing zeroes, cheyney's method will work fine - I thought it was quite neat actually :)
 
Ah! Right! I see it now. Quite neat indeed. A casual glance made me think that it would have killed inside zeroes as well. [blush]
 
Thank you, cheyney!
It works, replaces all zeros (in middle of the string too)with ' ', then trims trailing zeros, then puts back all zeros in the middle of the string.
From input 20379800 I get 203798, exactly what I need.
Thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top