Difference between Stuff and Replace

What is STUFF and Replace. Differece between them.

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 ,character_expression )
Usage in Real life Example :
SELECT STUFF('abcdef', 2, 3, 'ijklmn');
GO
Output ::
----------------------------------------------------------------------------------------
aijklmnef
(1 row(s) affected)
Command Highlights :
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.
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 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.
Replace ::
-------------------------------------------------------------------------------
Replaces all occurrences of a specified string value with another string value.

Syntax ::
-------------------------------------------------------------------------------
REPLACE (string_expression,string_pattern,string_replacement)

-------------------------------------------------------------------------------
Examples
--------------------------------------------------------------------------------

The following example replaces the string cde in abcdefghi with xxx.
CopySELECT REPLACE('abcdefghicde','cde','xxx');
GO
Here is the result set.
Copy------------
abxxxfghixxx

The following example uses the COLLATE function.

SELECT REPLACE('Sonal start walking' COLLATE Latin1_General_BIN,'l', 'O' );
GO
Here is the result set.
SonaO start wOking
---------------------------------------------------------------------------
Arguments
--------------------------------------------------------------------------------
string_expression
Is the string expression 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 ('').
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.

Comments

Popular posts from this blog

sp_addmessage can be used to create a User-Defined Error Message

Command-line Building With csc.exe

Prepare DTO layer by using below script