Tuesday, February 24, 2015

Stuff Function In SQL Server.

As per Microsoft BOL 
"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."

The syntax of the STUFF string function is as follows:

STUFF ( character_expression , start , length , replaceWith_expression )

Arguments
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.
replaceWith_expression
Is an expression of character data. character_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.

The following example returns a character string created by deleting three characters from the first string,VimtyeumarPrajapati, starting at position 4, at t, and inserting the second string at the deletion point.

select Stuff('VimtyeumarPrajapati',4,3 ,  'alK')
Go

Output:
VimalKumarPrajapati


Thanks!!!

No comments:

Post a Comment