Remove 3 characters from the beginning of a string and replace them with “HTML”.
SELECT STUFF(‘SQL Tutorial’, 1, 3, ‘HTML’); |
The STUFF() function in SQL deletes a specified length of characters from a string and replaces them with another string at a specified starting position.
Tip: Consider using the REPLACE() function as well.
STUFF(string, start, length, new_string) |
Parameter |
Description |
string |
Required: Specifies the string that will be altered. |
start |
Required: Specifies the starting position within the string from where characters will be deleted. |
length |
Required: Specifies the number of characters to remove from the string. |
new_string |
Required: Specifies the new string that will replace the deleted characters at the specified start position in the original string. |
Works in: |
SQL Server (from 2008 onward), Azure SQL Database, Azure SQL Data Warehouse, and Parallel Data Warehouse |
Remove 1 character from position 13 of a string and replace it with ” is fun!”.
SELECT STUFF(‘SQL Tutorial!’, 13, 1, ‘ is fun!’); |