Sunday, October 10, 2021

SQL Server Replace()

 Returns a string (varchar or nvarchar) where all occurrences of a specified sub string is replaced with another substring.

Parameters:

1. string expression. This is the string that would be searched. It can be a character or binary data type.

2. pattern. This is the sub string that would be replaced. It can be a character or binary data type. The pattern argument cannot be an empty string.

3. replacement. This is the sub string that would replace the pattern sub string. It can be a character or binary data.

SELECT REPLACE('This is my string', 'is', 'XX') -- Returns 'ThXX XX my string'.

Notes:

  • If string expression is not of type varchar(max) or nvarchar(max), the replace function truncates the return value at 8,000 chars.
  • Return data type depends on input data types - returns nvarchar if one of the input values is nvarchar, or varchar otherwise.
  • Return NULL if any of the input parameters is NULL

No comments:

Post a Comment