Articles → SQL SERVER → Stuff And Replace Functions In SQL Server

Stuff And Replace Functions In SQL Server






Software Requirement





Prerequisite Knowledge




  1. The basics of SQL Server objects such as tables, views, functions, etc.
  2. The difference between user-defined and system-defined functions

Replace Function






Picture showing how the replace function works in sql server





Syntax


SELECT 
  REPLACE(
    'This is the original string', 'is', 
    'was'
  )




  1. The first parameter is the string to be searched.
  2. The second parameter is the string to be found.
  3. The third one is the string value which will be replaced.

Stuff Function






select 
  stuff(
    'This is the original string', 6, 
    2, 'was'
  )




  1. The first parameter is the string from which characters will be deleted and the new characters will be inserted.
  2. The second parameter specifies the starting position where the string will be inserted.
  3. The third parameter is the number of characters to delete from the start position.
  4. The fourth parameter is the new string that will be inserted at the start position.
Picture showing how the stuff function works in SQL server





More Examples For Stuff Function


select 
  stuff('hello', 6, 2, 'how are you')




select 
  stuff('hello ', 6, 2, 'how are you')





Posted By  -  Karan Gupta
 
Posted On  -  Sunday, January 6, 2013

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250