totn SQL Server Functions

SQL Server: REPLACE Function

This SQL Server tutorial explains how to use the REPLACE function in SQL Server (Transact-SQL) with syntax and examples.

Description

In SQL Server (Transact-SQL), the REPLACE function replaces a sequence of characters in a string with another set of characters, not case-sensitive.

Syntax

The syntax for the REPLACE function in SQL Server (Transact-SQL) is:

REPLACE( string1, string_to_replace, replacement_string )

Parameters or Arguments

string1
The source string from which a sequence of characters will be replaced by another set of characters.
string_to_replace
The string that will be searched for in string1.
replacement_string
The replacement string. All occurrences of string_to_replace will be replaced with replacement_string in string1.

Note

  • The REPLACE function performs a replacement that is not case-sensitive. So all occurrences of string_to_replace will be replaced with replacement_string regardless of the case of string_to_replace or replacement_string.
  • See also the STUFF function.

Applies To

The REPLACE function can be used in the following versions of SQL Server (Transact-SQL):

  • SQL Server 2017, SQL Server 2016, SQL Server 2014, SQL Server 2012, SQL Server 2008 R2, SQL Server 2008, SQL Server 2005

Example

Let's look at some SQL Server REPLACE function examples and explore how to use the REPLACE function in SQL Server (Transact-SQL).

For example:

SELECT REPLACE('TechOnTheNet.com', 'T', '3');
Result: '3echOn3heNe3.com'          (both T and t are replaced by 3)

SELECT REPLACE('TechOnTheNet.com', 'Tech', '1234');
Result: '1234OnTheNet.com'

SELECT REPLACE('TechOnTheNet.com', 'c', '123');
Result: 'Te123hOnTheNet.123om'

SELECT REPLACE('Tech On The Net', ' ', 'Z');
Result: 'TechZOnZTheZNet'