Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialGiuseppe Ardito
14,130 PointsReplacement functions with multiple conditions
Hi all!
Does the REPLACE() function support multiple conditions - multiple targets and multiple values to replace - within the same column?
What's the best way to replacement multiple bit of a string in a column, selecting the column only one time?
Thanks
1 Answer
Shadd Anderson
Treehouse Project ReviewerHello,
Do you mean REPLACE()? :)
Yes, you can do this, but you'll have to nest it, for instance:
REPLACE(REPLACE(REPLACE(columnname, 'string1', 'string4'),'string2', 'string5'),'string3','string6')))
Giuseppe Ardito
14,130 PointsGiuseppe Ardito
14,130 PointsYes REPLACE()!
I rectified the question so it should be correct. Thanks for clarifying!
So in the nested REPLACE() the "column" parameter can be omitted?
Steven Parker
231,198 PointsSteven Parker
231,198 PointsThe "column" parameter cannot be omitted, but you can substitute a string or a function that returns a string for a column name.
You'll notice in Shadd's nested example that the inner-most function has a column for the first argument, but the others all have the next nested "replace" function as the first argument.
But you don't need to use nesting if you only want to replace multiple instances of the same string. For example:
SELECT REPLACE('This is my fist.', 'is', 'xx'); --> Thxx xx my fxxt.