Use case when
we are working on TSQL or Query ,some time we came such a situation when we need to
replace a set of string into another set of string, this can be anything like
string, character, numeric or special character. We can handle such situation
using STUFF function.
STUFF
The STUFF function inserts a string into another string. It deletes a specified length of characters in the first string at the start position and then inserts the second string into the first string at the start position.
Syntax:-
STUFF (Character Expression, Start, Length, Replace with
Expression)
Arguments: This
function uses the following parameters.
Character Expression: Is an expression of character data. Character
Expression can be a constant, variable, or column of either character or binary
data.
Start: Is an integer value
that specifies the location to start deletion and insertion. If start or length
is negative, a null string is returned. If start is longer than the first
Character Expression, a null string is returned. Start can be of type bigint.
Length: Is an integer that
specifies the number of characters to delete. If length is longer than the
first Character Expression, deletion occurs up to the last character in the last Character
Expression. Length can be of type bigint.
Replace With Expression: Is an expression of character data. Replace with Expression can
be a constant, variable, or column of either character or binary data. This
expression will replace length characters of Character
Expression beginning at start.
Return Types
Returns character data
if character expression is one of the supported character data types.
Returns binary data if character expression is one of the supported
binary data types.
Remarks
If the start position
or the length is negative, or if the starting position is larger than length of
the first string, a null string is returned. If the start position is 0, a null
value is returned. If the length to delete is longer than the first string, it
is deleted to the first character in the first string.
An error is raised if the resulting value is
larger than the maximum supported by the return type.
EXAMPLE
SELECT
STUFF(‘abcdefgh’, 2, 3, 'xyzwsq');
GO
OUTPUT
-------------------
axyzwsqbcdefgh
(1
row(s) affected)
No comments:
Post a Comment