views:

37

answers:

2

Hello All,

I have a string as follows - MFMFMF

now i want to change this string to FMFMFM how to do this , help needed pls

i had tried

select replace(replace('mfmfmf','M','F'),'F','M') this gives me result - MMMMMM which i donot what i want the output to be FMFMFM Need your help

D.Mahesh

+3  A: 

Try:

select replace(replace(replace('mfmfmf', 'm', 'x'), 'f', 'm'), 'x', 'f') ...

It's because your first replace yields:

ffffff

And then replacing fs with ms, yields mmmmmm. You need an intermediary replace.

xyld
Hello xyld,Thanks it workedD.Mahesh
mahesh
Can you hit the "checkmark" next to this answer then?
xyld
I don't think he expects the strings to be the same each time. Which means he'll eventually end up with a string that contains an x, in which case your code blows MF-ing up.
Will
I don't presume to understand exactly what hes trying to handle, however, I attempted to teach him enough about the problem so he can come up with his own solution in that situation.
xyld
+1  A: 

select replace(replace(replace('mfmfmf','M','X'),'F','M'),'X','F')

dnn
heh, weird how we both chose `x` as the intermediary value and switched `m` with `x`...
xyld
Weird how you both have the same bug, heheh.
Will