SoFunction
Updated on 2025-04-11

The usage and application scenarios of SQL Server STUFF function

In SQL Server,STUFFFunctions are powerful string processing tools, often used to delete characters at specified locations and insert new characters. Through this function, developers can flexibly modify strings, thereby showing off their skills in data processing, string splicing and formatting. This article will discuss in depthSTUFFFunction syntax, usage and common application scenarios.

Syntax of STUFF function

STUFF (string_expression, start, length, replace_with_expression)
  • twist_expression: The original string that needs to be modified.
  • start: indicates where to start the modification in the original string. This position starts counting from 1.
  • length: The number of characters to be deleted.
  • replace_with_expression: The inserted new string. Can be an empty string.

Basic use of STUFF functions

STUFFThe main function of the function is to delete a part of the content in the original string and replace it with the specified new content. We can perform a variety of string operations through this function, such as formatting, replacing characters, and splicing strings.

1. Delete and replace part of the string

STUFFOne of the most common uses of functions is to delete certain characters in a string and replace them with new characters.

Example:

SELECT STUFF('Hello World', 7, 5, 'SQL Server');

result:

Hello SQL Server

explain:

  • From string'Hello World'The 7th character of  (i.e."W")start,
  • Delete 5 characters (i.e."World"),
  • Then insert"SQL Server"

2. Delete some contents in the string

If we don't want to insert anything, but just delete a part of the string, we can turnreplace_with_expressionSet to empty string''

Example:

SELECT STUFF('abcdefg', 3, 2, '');

3. Use STUFF to implement string replacement

STUFFFunctions are often used to implement substitutions of certain parts in strings, especially when it is necessary to delete a part and insert another part.

Example:

SELECT STUFF('The quick brown fox', 5, 6, 'fast');

Advanced applications of STUFF functions

In addition to simple string modifications,STUFFFunctions can also be used in conjunction with other SQL techniques to handle more complex requirements.

1. String aggregation

STUFFFunctions andFOR XML PATHWhen combined, it can achieve similarGROUP_CONCATThe function of   combines strings in multiple lines into one string. This is useful when you need to merge the contents of multiple rows into one result.

Example:

SELECT STUFF(
    (SELECT ',' + name FROM Employees FOR XML PATH('')), 
    1, 1, ''
) AS EmployeeNames;

result:

Alice,Bob,Charlie,David

2. Format the data

When handling some formatting tasks,STUFFFunctions are also very useful. For example, format sensitive data such as phone numbers and ID numbers.

Example: Format phone number

SELECT STUFF('13812345678', 4, 4, '****') AS MaskedPhone;

result:

138****5678

explain:

  • Starting from the 4th character, delete 4 characters (i.e."1234"),
  • Insert****, and finally return the phone number with partial mask.

3. Replace special characters

STUFFIt can also be used to replace special characters in strings, especially during string formatting.

Example:

SELECT STUFF('2025-02-14', 5, 1, '/');

2025/02-14

explain:

  • From the 5th character (i.e."-")start,
  • Delete 1 character (i.e."-"),
  • Insert"/", the final format becomes2025/02-14

Application scenarios of STUFF function

1. Data cleaning and conversion

In daily database development, we often need to clean or convert strings.STUFFFunctions are very suitable for this task. For example, delete unnecessary characters, replace irregular characters, or format data.

2. Data splicing and aggregation

When multiple rows of data need to be spliced ​​into a long string,STUFFFunctions andFOR XML PATHCombined use, string aggregation can be easily achieved, avoiding multiple uses.CONCATOr other troubles of splicing.

3. Masking sensitive data

STUFFFunctions can be used to mask sensitive data (such as ID number, bank account, etc.). For example, retaining part of the information, masking the rest, helping to provide a partial view of the data without exposing too much information.

4. Data formatting

When processing date, time or other formatting requirements,STUFFFunctions can easily replace separators and adjust the display format of data. For example, modify the separator of the date, the format of the phone number, etc.

STUFFFunctions are very practical string processing functions in SQL Server, which can delete characters at specified locations and insert new characters. It not only helps developers modify strings, but also handles complex string stitching and aggregation tasks. By combining with other SQL features,STUFFIt can play an important role in multiple scenarios such as data cleaning, data formatting, aggregation and sensitive data processing. Therefore, master it proficientlySTUFFThe use of functions can greatly improve development efficiency and code quality.

This is the article about the usage and application scenarios of SQL Server STUFF functions. For more related SQL STUFF functions, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!