STUFF function
What is the STUFF function and how does it differ from the REPLACE function?
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Satyapriya NayakRank #47 · 31.5K reputationPosted Feb 1, 2012, 2:44 AM EST
STUFF function is used to insert a string into another string by deleting some characters specified.
The function below inserts the string "nny" at the 2nd position and replaces a total of 3 characters.
Example:
SELECT STUFF('john', 2, 3, 'nny')
Output:
jnny
On the other hand, REPLACE instead of replacing specific characters, replaces existing characters of all occurrences.
Example:
SELECT REPLACE ('Johnnohneny','ohn','ccc');
Output:
Jcccnccceny
Thanks
Jignesh TrivediRank #16 · 50.3K reputationPosted Feb 1, 2012, 5:30 PM EST
STUFF function is used to overwrite existing characters.
Using this syntax, STUFF (string_expression,start,length,replacement_characters)
where
string_expression is the string that will have characters substituted,
start is the starting position,
length is the number of characters in the string that are substituted, and replacement_characters are the new characters interjected into the string.
REPLACE function to replace existing characters of all occurrences.
Using the syntax REPLACE (string_expression, search_string, replacement_string), where every incidence of search_string found in the string_expression will be replaced with replacement_string.
hope this help.