Skip to content

Latest commit

 

History

History
128 lines (96 loc) · 5 KB

replace-transact-sql.md

File metadata and controls

128 lines (96 loc) · 5 KB
title description author ms.author ms.date ms.service ms.subservice ms.topic f1_keywords helpviewer_keywords dev_langs monikerRange
REPLACE (Transact-SQL)
Transact-SQL reference for the REPLACE function, which replaces all occurrences of a specified string value with another string value.
MikeRayMSFT
mikeray
08/23/2017
sql
t-sql
reference
REPLACE_TSQL
REPLACE
first string expression [SQL Server]
replacing string expression
third string expressions [SQL Server]
second string expressions [SQL Server]
REPLACE function
TSQL
>= aps-pdw-2016 || = azuresqldb-current || = azure-sqldw-latest || >= sql-server-2016 || >= sql-server-linux-2017 || = azuresqldb-mi-current||=fabric

REPLACE (Transact-SQL)

[!INCLUDE sql-asdb-asdbmi-asa-pdw-fabricse-fabricdw]

Replaces all occurrences of a specified string value with another string value.

:::image type="icon" source="../../includes/media/topic-link-icon.svg" border="false"::: Transact-SQL syntax conventions

Syntax

REPLACE ( string_expression , string_pattern , string_replacement )  

Arguments

string_expression
Is the string expression to be searched. string_expression can be of a character or binary data type.

string_pattern
Is the substring to be found. string_pattern can be of a character or binary data type. string_pattern must not exceed the maximum number of bytes that fits on a page. If string_pattern is an empty string (''), string_expression is returned unchanged.

string_replacement
Is the replacement string. string_replacement can be of a character or binary data type.

Return Types

Returns nvarchar if one of the input arguments is of the nvarchar data type; otherwise, REPLACE returns varchar.

Returns NULL if any one of the arguments is NULL.

If string_expression is not of type varchar(max) or nvarchar(max), REPLACE truncates the return value at 8,000 bytes. To return values greater than 8,000 bytes, string_expression must be explicitly cast to a large-value data type.

Remarks

REPLACE performs comparisons based on the collation of the input. To perform a comparison in a specified collation, you can use COLLATE to apply an explicit collation to the input.

0x0000 (char(0)) is an undefined character in Windows collations and cannot be included in REPLACE.

Examples

The following example replaces the string cde in abcdefghicde with xxx.

SELECT REPLACE('abcdefghicde','cde','xxx');  
GO  

[!INCLUDEssResult]

------------  
abxxxfghixxx  
(1 row(s) affected)  

The following example uses the COLLATE function.

SELECT REPLACE('This is a Test'  COLLATE Latin1_General_BIN,  
'Test', 'desk' );  
GO  

[!INCLUDEssResult]

------------  
This is a desk  
(1 row(s) affected)  

The following example calculates the number of spaces in a sentence using the REPLACE function. First, it calculates the length of the sentence with the LEN function. It then replaces the ' ' characters with '' with REPLACE. After this process, it calculates the length of the sentence again. The resulting difference is the number of space characters in the sentence.

DECLARE @STR NVARCHAR(100), @LEN1 INT, @LEN2 INT;
SET @STR = N'This is a sentence with spaces in it.';
SET @LEN1 = LEN(@STR);
SET @STR = REPLACE(@STR, N' ', N'');
SET @LEN2 = LEN(@STR);
SELECT N'Number of spaces in the string: ' + CONVERT(NVARCHAR(20), @LEN1 - @LEN2);

GO  

[!INCLUDEssResult]

------------  
Number of spaces in the sentence: 7  

(1 row(s) affected)  

See Also

CONCAT (Transact-SQL)
CONCAT_WS (Transact-SQL)
FORMATMESSAGE (Transact-SQL)
QUOTENAME (Transact-SQL)
REVERSE (Transact-SQL)
STRING_AGG (Transact-SQL)
STRING_ESCAPE (Transact-SQL)
STUFF (Transact-SQL)
TRANSLATE (Transact-SQL)
Data Types (Transact-SQL)
String Functions (Transact-SQL)