SQL Server: REPLACE Function

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 functions 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 functions examples and explore how to use the REPLACE functions in SQL Server (Transact-SQL).

For example:

SELECT REPLACE('adglob.in', 'l', '3');
Result: 'adg3ob.in'          (both l and L are replaced by 3)

SELECT REPLACE('adglob.in', 'adgl', '1234');
Result: '1234ob.in'

SELECT REPLACE('adglob.in', 'g', '123');
Result: 'ad123lob.in'

This Post Has One Comment

Leave a Reply