0

How to Convert the SQL data type Numerci(15,2) to string(varchar data type) without adding the trailing zeros in sybase.

Example- in column abc below values are present-

0.025
0.02
NULL
0.025589
5.289

on running the query-

select STR(column,10,4) from table --- produces the results 0.025,0.0200
select CAST(column as CHAR(5)) from table -- produces the results as 0.0250 etc

I can not do it in presentation layer Can someone please help with query.

Ali Azam
  • 2,047
  • 1
  • 16
  • 25
RGoyal
  • 165
  • 3
  • 16

2 Answers2

0

Unfortunately Sybase ASE does not have any native support for regex's, nor any out-of-the-box functions for stripping trailing zeros.

An obvious (?) first attempt might consist of a looping construct to strip off the trailing zeros, though it'd probably be easier to reverse() the initial string, strip off leading zeros, then reverse() to get back to the original value. Unfortunately this is not exactly efficient and would need to be encapsulated in a user-defined function (which introduces an additional performance hit each time it's invoked) in order to use it in a query.

The next idea would be to convert the zeros into something that can be (relatively) easily stripped off the end of the string, and it just so happens that ASE does provide the rtrim() function for stripping trailing spaces. This idea would look something like:

  • convert all zeros to spaces [str_replace('string','0',' ')]
  • strip off trailing spaces [rtrim('string')]
  • convert any remaining spaces back to zeros [str_replace('string',' ','0')]

** This obviously assumes the original string does not contain any spaces.

Here's an example:

declare @mystring varchar(100)

select  @mystring = '0.025000'

-- here's a breakdown of each step in the process ...

select ':'+                               @mystring                    + ':' union all
select ':'+                   str_replace(@mystring,'0',' ')           + ':' union all
select ':'+             rtrim(str_replace(@mystring,'0',' '))          + ':' union all
select ':'+ str_replace(rtrim(str_replace(@mystring,'0',' ')),' ','0') + ':'

-- and the final solution sans the visual end markers (:)

select str_replace(rtrim(str_replace(@mystring,'0',' ')),' ','0')
go

 ----------
 :0.025000:
 : . 25   :
 : . 25:
 :0.025:

 --------
 0.025

If you need to use this code snippet quite often then you may want to consider wrapping it in a user-defined function, though keep in mind there will be a slight performance hit each time the function is called.

markp-fuso
  • 28,790
  • 4
  • 16
  • 36
-1

Following approaches can be used - 1) It uses the Replace function

select COLUMN,str_replace(rtrim(str_replace(
       str_replace(rtrim(str_replace(cast(COLUMN as varchar(15)), '0', ' ')), ' ', '0')
       , '.', ' ')), ' ', '.')
from TABLE

Output -

0.025
2


0.025
2

2) Using Regex-

select COLUMN ,str(COLUMN ,10,3),  
    reverse(substring( reverse(str(COLUMN ,10,3)), patindex('%[^0]%',reverse(str(COLUMN ,10,3))), 10)) 
from TABLE

Output -

0.025
2


0.025
2.

RGoyal
  • 165
  • 3
  • 16