Questions tagged [stuff]

The STUFF function inserts a string into another string. It deletes a specified length of characters in the first string at the start position and then inserts the second string into the first string at the start position.

Syntax

STUFF ( character_expression , start , length , replaceWith_expression )

Arguments

character_expression

Is an expression of character data. character_expression can be a constant, variable, or column of either character or binary data.

start

Is an integer value that specifies the location to start deletion and insertion. If start is negative or zero, a null string is returned. If start is longer than the first character_expression, a null string is returned. start can be of type bigint.

length

Is an integer that specifies the number of characters to delete. If length is negative, a null string is returned. If length is longer than the first character_expression, deletion occurs up to the last character in the last character_expression. If length is zero, insertion occurs at start location and no characters are deleted. length can be of type bigint.

replaceWith_expression

Is an expression of character data. character_expression can be a constant, variable, or column of either character or binary data. This expression replaces length characters of character_expression beginning at start. Providing NULL as the replaceWith_expression, removes characters without inserting anything.

Return Types

Returns character data if character_expression is one of the supported character data types. Returns binary data if character_expression is one of the supported binary data types.

Remarks

If the start position or the length is negative, or if the starting position is larger than length of the first string, a null string is returned. If the start position is 0, a null value is returned. If the length to delete is longer than the first string, it is deleted to the first character in the first string.

An error is raised if the resulting value is larger than the maximum supported by the return type.

Official Documentation

MSDN

48 questions
-2
votes
1 answer

SQL Server STUFF query with the SAME table

I need to get the result like this: I have tried using this query: SELECT STUFF((SELECT ', ' + CONVERT(VARCHAR(50), RuleNumber) FROM #tempSelectPlusReferralsExtracts v2 WHERE v2.RuleApprovedDate IN (CASE…
Kapil
  • 1,823
  • 6
  • 25
  • 47
-3
votes
2 answers

Substitute for Function STUFF (SQL Server) in AWS redshift

I have to replace first 3 digits of a column to a fix first 3 digits (123) Working SQL Server code. (Not working on AWS RedShift) Code: Select Stuff (ColName,1,3,'123')as NewColName From DataBase.dbo.TableName eg 1 -Input --- …
1 2 3
4