bassmo Posted March 12, 2012 Share Posted March 12, 2012 Hi all, I have a rather annoying problem. I need to be able to run a replace on a string. In itself obviously not a problem. However I only need to replace the occurrence of the string when it is at the start of the string. Example: Need to replace Apple with Orange: AppleBananaPeach to become OrangeBananaPeach ApplePearPlumApple to become OrangePearPlumApple BananaApple to remain BananaApple All strings are a single "word", no spaces. The thought occurs to me to place a marker at the start. Something along the lines of: |ApplePearPlumApple -> Replace |Apple with Orange -> OrangePearPlumApple Any idea if I can do this? Many thanks in advance. Bassmo / Jon Link to comment https://forums.phpfreaks.com/topic/258746-tricky-query-helpis-this-even-possible-i-love-laying-down-a-challenge/ Share on other sites More sharing options...
bassmo Posted March 12, 2012 Author Share Posted March 12, 2012 Actually not tricky - just done it myself....sorry update testtable set myfield = replace(concat('|',myfield,'|apple','orange'); Link to comment https://forums.phpfreaks.com/topic/258746-tricky-query-helpis-this-even-possible-i-love-laying-down-a-challenge/#findComment-1326452 Share on other sites More sharing options...
Muddy_Funster Posted March 12, 2012 Share Posted March 12, 2012 Or if you only want to show a different word without changing the data in the table you could use this, handy for masking the start of sensetive information, such as card no's and the like SELECT REPLACE((SELECT `text` FROM table where left(`text`, Length('WordToReplace')) = 'WordToReplace'), 'WordToReplace', 'NewWord') Link to comment https://forums.phpfreaks.com/topic/258746-tricky-query-helpis-this-even-possible-i-love-laying-down-a-challenge/#findComment-1326458 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.