Stuff and Replace – New SQL Server functions for DBA and Programmers

Posted In Microsoft - By Piyush Dungrani On Wednesday, September 22nd, 2010 With 0 Comments

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('HelloWorld', 6, 0, ' is good for all ') as Example;

Output ::

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.
SELECT REPLACE('HelloWorld','l','$')
GO
Here is the result set.

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.

Incoming search terms:

difference between stuff and replace function in sql server, difference between stuff and replace in sql server 2008, difference between replace and stuff in sql server, Replace function and stuff function difference, stuff function in sql server 2008 example, example of stuff in sql server, stuff replace, sql stuff example, t sql stuff parameters, sql server stuff example, sql server stuff examples, sql stuff binary string, sql stuff function to return in string, sql stuff sql, what is stuff function in sql server 2008

About - A techie by profession (software engineer) and a part time blogger by choice with immense knowledge of SEO, also writes articles on Google, Microsoft, Apple, iPhone, Internet, Blogger, Social Media and windows. Catch him on Twitter, Join Facebook Fan Page. Subscribe to GeekBlogger feed via RSS or EMAIL to receive instant updates.

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*