C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » Databases »

Difference between STUFF function and Replace Function in SQL ?


Posted Date: 21 Jun 2009    Resource Type: Articles    Category: Databases
Author: Viji RAJKUMARMember Level: Diamond    
Rating: 1 out of 5Points: 10



The STUFF function is used to overwrite the characters of string.

The Replace function is used to replace the occurance of a particular string with the specified string in all occurances.


STUFF Syntax:

STUFF (String, StartPos, LengthofReplaceChar, ReplaceString)

String - String to be overwritten
StartPos - Starting Position for overwriting
LengthofReplaceChar - Length of replacement string
ReplaceString - String to overwrite

Example


Declare @OriginalString Varchar(100)
Declare @ResultString Varchar (100)
Declare @ReplaceString Varchar(100)

SET @OriginalString = 'ALAMO'
SET @ReplaceString = 'Welcome '

SET @ResultString = SELECT STUFF(@OriginalString, 1,7, @ReplaceString)

Print 'Result: ' + @ResultString




Output

It will erase the letter A(First Letter) and inject 'Welcome ' in that position

Result: Welcome Lamo


REPLACE Syntax:

REPLACE (String, StringToReplace,StringTobeReplaced)

String - Input String
StringToReplace - The portion of string to replace
StringTobeReplaced - String to overwrite


Example


Declare @OriginalString Varchar(100)
Declare @ResultString Varchar (100)
Declare @ReplaceString1 Varchar(100)
Declare @ReplaceString2 Varchar(100)

SET @OriginalString = 'ALAMO'
SET @ReplaceString1 = 'A'
SET @ReplaceString2 = 'V'


SET @ResultString = SELECT REPLACE(@OriginalString, @ReplaceString1, @ReplaceString2)

Print 'Result: ' + @ResultString



Output

It will replace the letter A in the whole string

Result: VLVMO





Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Difference between STUFF function and Replace Function in SQL ?  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Data Types in SQL Server.
Previous Resource: SQL : INSERT Multiple records using One INSERT INTO Statement
Return to Discussion Resource Index
Post New Resource
Category: Databases


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use