Stuff and Replace Function in SQL Server : Detailed Description

(This is a guest blog post by Daniel Jones (li | t | fb). Daniel is a SQL Server DBA and contributor at SQL Tech Tips. Having 2 + years of experience in SQL recovery and system infrastructure.)

In SQL Server, STUFF and REPLACE functions are used to replace the characters in a string. Both functions play an important role in Transact-SQL with performing the distinct functionality. In the following section, we are going to discuss about Stuff and Replace function in detail.

STUFF Function in SQL Server

In SQL Server, the STUFF function is utilized to replace the part of the string with some another string. Moreover it can remove the specified length of the characters in the first string and after that, insert the second string into the first string at the starting position.

Syntax of STUFF Function

STUFF ( character_expression , start , length , replaceWith_expression )

Parameters

    • character_expression

This argument can be a constant, variable or may be the column of either binary or character data

    • start

It is an integer value that indicates the location from where to begin the deletion and insertion as well. In case, if start or length parameters may be negative then, it will return a null string. Besides, if start is used longer than the first-character_expression, again null string is returned. It also can be of bigint type.

    • length

Also, it is an integer that denotes the number of characters to remove. If using length longer than the first character_expression, then, deletion happens to the last character within the last character_expression. It could be of type bigint.

    • replaceWith_expression

This attribute represents same as character_expression. Even it may be a variable, constant, or columns of character or binary data. Even this expression can replace length characters from character_expression in the beginning at start

Return Types

In case, if character_expression is one of the supported character data, returns the character type data. Otherwise, gives binary data if character_expression is a single supported binary data types.

Remarks

In the above parameters, if the length or start integer is negative, or if the start position is larger than the length of the primary string, then, returns the null string. Although, returns null value if the starting position is zero. If the length is longer than the first string, it has decided to remove the first character data type in the first string.

Replace Function in SQL Server

As the above function name indicates, the REPLACE function in SQL server allows to replace all the occurrences of a specified string value with another set of the string value.

Syntax of REPLACE Function

REPLACE ( string_expression , string_pattern , string_replacement )

Parameters

    • string_expression

This string expression to be searched. Either it can be of character data or binary data type.

    • string_pattern

It can also be of a binary as well as character data type. In fact, it cannot be an empty string and may not exceed the maximum number of bytes that is suitable on a page.

    • string_replacement

A string_replacement argument can be of any data type either character or binary as well.

Return Types

If one of the input parameters is of nvarchar type then, returns nvarchar value; else, REPLACE gives varchar value.

In addition, if any individual argument is NULL then, returns the NULL value.

Moreover, if string-expression cannot be of type varchar or nvarchar, then, REPLACE function truncates the return range at 8,000 bytes. To provide values larger than 8,000 bytes, even string-expression may be cast explicitly to a greater value data type.

Remarks

REPLACE function performs the comparison of the input, which is based on collation. In a specified collation performing a comparison, also users can use COLLATE to apply collation to the desired input.

Char(zero) is an undefined character in collation and unable to include in REPLACE function.

Conclusion

STUFF and REPLACE are two major SQL Server functions that have discussed in above section. Moreover, to understand the various aspects of both the functions, we have presented the syntax and parameters in detail.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.