Introduction

In this article, I explain most SQL Server functions for manipulating text.

The list of functions described in this article is.

Sr. No Function Sr. No Function
1. CHARINDEX 10. REVERSE
2. PATINDEX 11. REPLACE
3. LEFT 12. STUFF
4. RIGHT 13. REPLICATE
5. LEN 14. QUOTENAME
6. LTRIM 15. STR
7. RTRIM 16. SUBSTRING
8. LOWER 17. SPACE
9. UPPER


Explanation of Functions

Here I am explaining each function mentioned in the above table.

CHARINDEX

We can use the CHARINDEX function to search text in a string expression. It returns the first occurrence of text searched for in another text expression or returned 0 if there is no match. It takes three arguments.

Syntax

CHARINDEX(searchExperision, textExpresion, startIndex)

The following explains each argument.

Example

img1.jpg

Output

img2.jpg

PATINDEX

We can use the PATINDEX function to search text in a string expression. It takes two arguments. It returns the first occurrence of a text pattern searched for in another text expression or returned 0 if there is no match. We use a wild card to search for a character expression in this function.

Syntax

PATINDEX(searchExperision, textExpresion)

The following explains each parameter.

Example

img3.jpg

Output

img4.jpg

LEFT

It returns the text from the left portion of the character string with a fixed number of characters. It takes two parameters.

Syntax

LEFT(textExpression, noOfChar)

The following explains each argument.

Example

img5.jpg

Output

img6.jpg

RIGHT

It returns the text from the right portion of the character string with a fixed number of characters. It takes two arguments.

Syntax

RIGHT(textExpression, noOfChar)

The following explains each argument.

Example

img7.jpg

Output

img8.jpg

LEN

It returns the number of characters of the character expression. It excludes the right side (trailing) blanks but does not exclude the left side (leading) blanks when counting the length. It takes one argument.

Syntax

LEN(textExpression)

The following explains the argument.

Example

img9.jpg

Output

img10.jpg

LTRIM

It returns a character expression after removing left-side (trailing) blanks. It takes one argument and returns a character expression.

Syntax

LTRIM(textExpression)

The following explains the argument.

Example

img11.jpg

Output

img12.jpg

RTRIM

It returns a character expression after removing right-side (trailing) blanks. It takes one argument and returns a character expression.

Syntax

RTRIM(textExpression)

The following explains the argument.

Example

img13.jpg

Output

img14.jpg

LOWER

It returns a character expression. It converts all upper-case characters to lower-case ones. It takes one argument as a character expression which will be converted to lower-case characters.

Syntax

LOWER(textExpression)

The following explains the argument.

Example

img15.jpg

Output

img16.jpg

UPPER

It returns a character expression. It converts all lower-case characters to upper-case. It takes one argument as a character expression which will be converted to upper-case characters.

Syntax

UPPER(textExpression)

The following explains the argument.

Example

img17.jpg

Output

img18.jpg

REVERSE

It returns a character expression. This function reverses a character expression; in other words, it reverses the whole statement with each word. It not only reverses the character expression but also can reverse integer values. It takes one argument.

Syntax

REVERSE(textExpression)

The following explains the argument.

Example

img19.jpg

Output

img20.jpg

REPLACE

It replaces a string value in another value for all occurrences with a string value. It returns the string value after replacing it, and it takes three arguments.

Syntax

REPLACE(textExpresion, findExpresion, replaceExpresion)

The following explains each argument.

Example

img21.jpg

Output

img 22.jpg

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. A NULL string is returned if the start position or the length is negative. It takes four arguments.

Syntax

STUFF(textExpression, startPosition, length, insertExpression)

Now explains each argument.

Example

img 23.jpg

Output

img 24.jpg

REPLICATE

This function repeats a character expression a number of times; in other words, we want to repeat a string value several times in a character expression. It takes two arguments and returns a character expression.

Syntax

REPLICATE(textExpresion, integerExpression)

The following explains each argument.

Example

img 25.jpg

Output

img 26.jpg

QUOTENAME

The QUOTENAME function appends square brackets to the beginning and end of the string expression and thereby makes a string expression a valid SQL Server identifier. It takes one argument. But when we have a closing square bracket in the string expression, QUOTENAME appends an extra closing square bracket. The QUOTENAME function is useful when working with database object names that contain spaces and reserved words. Generally, using reserved words, special characters, and spaces inside your object names is a bad idea.

Syntax

QUOTENAME(textExpression)

Now explain the argument

Example

img27.jpg

Output

img 28.jpg

STR

The str function converts numbers (int, bigint, float, etc.) to characters. It takes three arguments for the number expression, the length of the number, and the number of places after the decimal point.

Syntax

STR(numberExpression, length, decimalNumber)

Now explains each argument.

Example

img 29.jpg

In the above example, the length is 6, and the total numeric expression length is 7, so the digits after the decimal point will be rounded off.

Output

img 30.jpg

Example

img 31.jpg

In the above example, the length is 2, and the total numeric expression length before the decimal point is 3, so the return value will be filled with "*" (asterisk).

Output

img 32.jpg

SUBSTRING

It returns the portion of the character expression from a string value. It takes three arguments.

Syntax

SUBSTRING(textExpression. startIndex, length)

Now explain each argument.

Example

img 33.jpg

Output

img 34.jpg

SPACE

It repeats the space character in a string. It takes one argument that represents how many spaces will be returned.

Syntax

SPACE(numericExpression)

Now explain the argument.

Example

img 35.jpg

Output

img 36.jpg

Summary

This article taught us about SQL Server Text Data Manipulation with different functions and example programs.