In this tutorial, I am going to explain you SQL-Server built-in String functions with example. Below is the list of most commonly used SQL-Server System defined String function.
We will discuss in this article | Already discussed in previous article |
|
|
---|
Below is explanation for each function.
CHARINDEX Function
Syntax:CHARINDEX ( expressionToFind , expressionToSearch, [ Start_location ] )Description:It is used to find index position of specified expression in a Given Expression.Start_location parameter is optional.Searching starts from first position if Start_location is not supplied.Returns Zero if specified expression is not found.Example:
SUBSTRING Function
Syntax:SUBSTRING ( expression ,start , length )Description:It is used to find part of string in given expression.Expression can be of character, binary, text, ntext, or image type.Example:
Combining LEN,CHARINDEX and SUBSTRING Functions
Here, we will use len, charindex and substring functions together as shown in below example.Example:
REPLICATE Function
Syntax:REPLICATE ( string_expression ,integer_expression )Description:It is used to repeat the given string for the specified number of times.Example:
SPACE Function
Syntax:SPACE ( integer_expression )Description:It is used return number of space, specified by the integer_expression.Example:
PATINDEX Function
Syntax:PATINDEX ( '%pattern%' , expression )Description:It is used to find starting position of the first occurrence of a pattern in a specified expression.It takes two arguments, the pattern to be searched and the expression.PATINDEX is similar to CHARINDEX, but we can not use wild-cards with CHARINDEX where as with PATINDEX we can use wild-cards.If the specified pattern is not found, PATINDEX returns ZERO.Example:
REPLACE Function
Syntax:REPLACE ( string_expression , string_pattern , string_replacement )Description:It is used to replace all occurrences of a specified string value with another string value.Example:
STUFF Function
Syntax:STUFF ( character_expression , start , length , replaceWith_expression )Description:It is used insert 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.Example: