|
|
Question : user function in sybase
|
|
Hi,
we are using sybase 11.5 Ver. we have a req. in which we have to expose one of numeric data with turnctaed trailing zero's.
for eg: If we have 0.87900000 . This should be displayed as 0.879 0.88756000 as 0.88756 and 0.88000000 as 0.88
Pls let us know how we can go ahead.
Thanks
Raj
|
Answer : user function in sybase
|
|
The following seems to do the trick on 15.0 -
select reverse(reverse(convert(varchar(10), mycolumn))) from mytable
but it may not be supportable (I'm not sure why convert() drops the trailing zeros).
I'd be much more confident that code like the following would be robust (the variables can be eliminated and everything combined on one line) to handle multiple rows:
create table foo (x float) go insert foo values (0.879000) declare @x varchar(10) declare @y int select @x = reverse(convert(varchar(10),x)) from foo select @x select @y = patindex("%[^0]%", @x) select @y select reverse(substring(@x,@y, 10))
(i.e convert value to a string, flip it, find the offset of the first non-zero value, substring out the remainder, flip that back to get the final value).
|
|
|
|
|