searches the string I talked the talk, replacing every occurrence of the substring alk with ank. Here are the results:
Before After
I talked the talk. I tanked the tank.
The length of the substring being replaced doesn’t have to match the length of the replacement substring, as this example shows
SELECT Movie, Stars,
REPLACE(Stars,'*','STAR')
AS 'Replaced'
FROM vMovieReviewSummary
ORDER BY Stars
Figure 4 shows the results.
A Little Research Can Save You a Lot of Time
When it comes to built-in T-SQL functions, I’ve barely scratched the surface. I hope that what I covered in this lesson will give you a basic understanding of how to use scalar functions in your own queries and where to find out about the other types of built-in functions. Don’t be like one of my colleagues who was shocked when I removed about 500 lines of buggy code from his stored procedure and replaced it with a single REPLACE command. It amazes me how much effort people pour into reinventing the wheel because they don’t bother to do a little research upfront. Me? I’m too lazy to be inefficient!