MS SQL Server PATINDEX() Function
Sometimes you may need to delete specific characters in one string by inserting another specific length of string in MS SQL Server. There is a function called STUFF
in MS SQL Server by which you can do this. 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..
Let’s see the function syntax:
STUFF(string_expression, start, length, replace_with_expression );
In this syntax:
- Parameter string_expression is an expression of character data. string_expression can be a constant, variable, or column of either character or binary data.
- Parameter 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 string_expression, a null string is returned. start can be of type bigint.
- Parameter 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 string_expression, deletion occurs up to the last character in the last string_expression. If length is zero, insertion occurs at start location and no characters are deleted. length can be of type bigint.
- Parameter replace_with_expression is an expression of character data. string_expression can be a constant, variable, or column of either character or binary data. This expression replaces length characters of string_expression beginning at start. Providing
NULL
as the replace_with_expression, removes characters without inserting anything. - Function returns character data if string_expression is one of the supported character data types. Returns binary data if string_expression is one of the supported binary data types. 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.
The following example uses STUFF
function to join the strings.
DECLARE @s varchar(30) = 'MS SQL Server'; DECLARE @r varchar(30) = 'icrosoft'; SELECT @s as original_text,STUFF(@s, 2, 1, @r) result_text;
Here is the result set.
original_text | result_text |
---|---|
MS SQL Server | Microsoft SQL Server |
In the above example STUFF function delete one character ‘S’ from the second position and replace it by inserting the second string ‘icrosoft’ at the deletion point.
I have an another post MS SQL Server String Related Functions where you will learn about different string related functions of MS SQL Server.
In this tutorial, I have shown you how to delete specific characters in one string by inserting another specific length of string by using STUFF in MS SQL Server. Hope you have enjoyed the tutorial. If you want to get updated, like my facebook page http://www.facebook.com/LearningBigDataAnalytics and stay connected.