Sql Server Replace Function

SQL Server: REPLACE Function

In this post 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('AODBA.com', 'A', '3');
Output: '3ODB3.com'          (both T and t are replaced by 3)