Answers

Question and Answer:

  Home  Basic SQL Server

⟩ Do you know what is REPLACE and STUFF function in SQL Server?

STUFF: This function is used to replace the part of string with some another string.

Syntax:

STUFF (String1, Position, Length, String2)

String1 - String to be overwritten

Position - Starting Position for overwriting

Length - Length of replacement string

String2- String to overwrite

Example:

SELECT STUFF(‘Arpit',2,2,'mi')

Output: Amit

REPLACE: This function is used to replace all the occurrences of particular string by another string.

Syntax:

REPLACE(String1, String2, String3)

Example:

SELECT REPLACE(‘Arpit Jain’,’i’,’m’)

Output: Arpmt Jamn

If you want to replace the first occurrence of “I”, Replace wouldn't work, because it always replaces ALL occurrences of the string.

 184 views

More Questions for you: