--- title: "REPLACE (Transact-SQL) | Microsoft Docs" description: "Transact-SQL reference for the REPLACE function, which replaces all occurrences of a specified string value with another string value." ms.date: "08/23/2017" ms.prod: sql ms.prod_service: "database-engine, sql-database, sql-data-warehouse, pdw" ms.reviewer: "" ms.technology: t-sql ms.topic: reference f1_keywords: - "REPLACE_TSQL" - "REPLACE" dev_langs: - "TSQL" helpviewer_keywords: - "first string expression [SQL Server]" - "replacing string expression" - "third string expressions [SQL Server]" - "second string expressions [SQL Server]" - "REPLACE function" ms.assetid: 8a7aaaf2-62e3-46c0-8e44-fa22290dd86b author: julieMSFT ms.author: jrasnick monikerRange: ">=aps-pdw-2016||=azuresqldb-current||=azure-sqldw-latest||>=sql-server-2016||>=sql-server-linux-2017||=azuresqldb-mi-current" --- # REPLACE (Transact-SQL) [!INCLUDE [sql-asdb-asdbmi-asa-pdw](../../includes/applies-to-version/sql-asdb-asdbmi-asa-pdw.md)] Replaces all occurrences of a specified string value with another string value. ![Topic link icon](../../database-engine/configure-windows/media/topic-link.gif "Topic link icon") [Transact-SQL Syntax Conventions](../../t-sql/language-elements/transact-sql-syntax-conventions-transact-sql.md) ## Syntax ```syntaxsql REPLACE ( string_expression , string_pattern , string_replacement ) ``` [!INCLUDE[sql-server-tsql-previous-offline-documentation](../../includes/sql-server-tsql-previous-offline-documentation.md)] ## Arguments *string_expression* Is the string [expression](../../t-sql/language-elements/expressions-transact-sql.md) 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* cannot be an empty string (''), and must not exceed the maximum number of bytes that fits on a page. *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](~/t-sql/statements/collations.md) 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 `abcdefghi` with `xxx`. ```sql SELECT REPLACE('abcdefghicde','cde','xxx'); GO ``` [!INCLUDE[ssResult](../../includes/ssresult-md.md)] ``` ------------ abxxxfghixxx (1 row(s) affected) ``` The following example uses the `COLLATE` function. ```sql SELECT REPLACE('This is a Test' COLLATE Latin1_General_BIN, 'Test', 'desk' ); GO ``` [!INCLUDE[ssResult](../../includes/ssresult-md.md)] ``` ------------ This is a desk (1 row(s) affected) ``` ## See Also [CONCAT (Transact-SQL)](../../t-sql/functions/concat-transact-sql.md) [CONCAT_WS (Transact-SQL)](../../t-sql/functions/concat-ws-transact-sql.md) [FORMATMESSAGE (Transact-SQL)](../../t-sql/functions/formatmessage-transact-sql.md) [QUOTENAME (Transact-SQL)](../../t-sql/functions/quotename-transact-sql.md) [REVERSE (Transact-SQL)](../../t-sql/functions/reverse-transact-sql.md) [STRING_AGG (Transact-SQL)](../../t-sql/functions/string-agg-transact-sql.md) [STRING_ESCAPE (Transact-SQL)](../../t-sql/functions/string-escape-transact-sql.md) [STUFF (Transact-SQL)](../../t-sql/functions/stuff-transact-sql.md) [TRANSLATE (Transact-SQL)](../../t-sql/functions/translate-transact-sql.md) [Data Types (Transact-SQL)](../../t-sql/data-types/data-types-transact-sql.md) [String Functions (Transact-SQL)](../../t-sql/functions/string-functions-transact-sql.md)