STUFF function in SQL Server

The article will learn and show you how to use STUFF function to insert a string into another string.

The article will explore and show you how to use the STUFF function to insert a string into another string in SQL Server.

Describe

STUFF function in SQL Server is used to insert a string into another string, the result returned is a new string after canceling some existing characters and adding another substring at the canceled location.

Syntax

To use STUFF function in SQL Server, we use the following syntax:

 STUFF(string, start, length, new_string) 

Parameters :

  1. string: the original string you want to change.
  2. start: position in the string to start deleting some characters
  3. length: the number of characters to delete from the string.
  4. new_string: string of characters to insert into the string at the start position.

Note :

  1. See also the REPLACE function for selecting and replacing strings.
  2. STUFF function can be used in later versions of SQL Server: SQL Server 2017, SQL Server 2016, SQL Server 2014, SQL Server 2012, SQL Server 2008 R2, SQL Server 2008, and SQL Server 2005.

For example

See and explore some examples of STUFF functions in SQL Server.

 SELECT STUFF('TipsMake.com', 1, 11, 'Meta'); 
Result: 'Meta.com'

SELECT STUFF('QuanTriMang.com', 5, 3, '1234');
Result: 'Quan1234Mang.com'

SELECT STUFF('QuanTriMang.com', 12, 4, ' la website thong tin dien tu');
Result: ' QuanTriMang la website thong tin dien tu'

Previous lesson: STR function in SQL Server

Next article: SUBSTRING function in SQL Server

4 ★ | 1 Vote