Tag: REPLACE function in SQL Server

REPLACE, REPLICATE and REVERSE functions in SQL Server

REPLACE in SQL Server

Replace function replaces a substring with another substring in a string.

Let us quickly look at the examples straightaway to understand better.

SELECT REPLACE('SQL Server is a database server', 'sql server', ' SQL SERVER');

Point to be noted is that REPLACE function replaces all occurrences of word to be replaced. If there is a requirement to replace first or any specific range, then REPLACE will not work. There is an old blog post that explains how to replace ONLY the first occurrence as https://sqlzealots.com/2017/05/20/how-to-replace-first-occurrence-of-a-word-in-a-sentence-in-sql-server/

REPLICATE in SQL Server

REPLICATE() function repeats a string to the number of times its specified in the second parameter.

SELECT REPLICATE('SQL | ',2)

REVERSE in SQL Server

REVERSE() function reverses a string passed.

SELECT REVERSE ('SQL Server')

Reverse is a very powerful SQL String function that uses many places. I used this function in one of my earlier post on checking palindrome in SQL Server as below.

https://sqlzealots.com/2020/10/04/palindrome-in-sql-server/

If you enjoyed this blog post, feel free to share it with your friends!