Question : replace function in sybase 5.5

Dear Experts,

i am using Sybase database engine version 5.5.

i need to use a replace function in a trigger to replace single quotations with 2 single quotations.. and i was shocked that replace,str_replace, translate.. do not exist.

i.e. select replace(description,"'","''") from products

is there any other alternative function to accomplish this task?? UDFs are also welcome if a built-in function does not exist...

i appreciate your help

thanks,

Dan

Answer : replace function in sybase 5.5

O.K., now down to solving your problem...

I would do this with a UDF that calls itself recursively.  It is not the fastest approach but it will work and not be a total bear to maintain.

The UDF should use the char_index function to locate and the substring function to drop an additional quote character into the string.  It then takes calls itself with the remainder of the string as the parameter.  This takes care of the situation where there is more than one single quote in the string that you have to fix.  The exit from the recursion is when no un-escaped single quotes are found.

In pseudo code, the function would be something like the snippet below.

Regards,

Bill

1:
2:
3:
4:
5:
6:
7:
8:
function rplquote(@string)
begin
  declare @firstQ int;
  @firstQ = char_index(@string,"'");
  if @firstQ = 0 then return(@string);
  @newstring = substring(@string,1,@firstQ) + "'" +
               rplquote(substring(@string,@firstQ+1,char_length(@string)))
end
Open in New Window Select All
Random Solutions  
 
programming4us programming4us