Introduction
In this post, we will discuss how to work with SQL string functions and explain the concepts with an example in a simple way. I hope this is very useful for beginners and intermediates to help them understand the basic concept.
Following are the list of SQL functions required to manipulate the string,
- ASCII()
- CHAR()
- CHARINDEX()
- DATALENGTH()
- LEFT()
- LEN()
- LOWER()
- LTRIM()
- REPLACE()
- RIGHT()
- RTRIM()
- STUFF()
- SUBSTRING()
- UPPER ()
- REVERSE()
Concept explanation with example,
ASCII()
The ASCII function returns numeric values of the left-most character of a string.
Example
- DECLARE @Name varchar(30)
- SET @Name = 'Developer'
- SELECT ASCII(@Name) AS NAME
The char function returns the ASCII character based on the number.
Example
- DECLARE @CHAR AS INT
- SET @CHAR = 68
- SELECT CHAR(@CHAR) AS CHARACTER
Output
CHARINDEX()
The CHARINDEX function returns the location of a substring in a string
Example
- select CHARINDEX('@', '[email protected]') As CHARINDEX

The CONCAT function returns concatenates of two or more strings together.
Example
- DECLARE @Str1 varchar(30), @Str2 varchar(30)
- SET @Str1 = 'Developer'
- SET @Str2 = 'Software '
- SELECT CONCAT(@Str2, @Str1) AS Result
Output

The DATALENGTH function returns the length of an expression (in bytes)
Example
- DECLARE @Str1 nvarchar(30), @Str2 varchar(30)
- SET @Str1 = 'Developer'
- SET @Str2 = 'Developer'
- SELECT DATALENGTH(@Str1) AS Result, DATALENGTH(@Str2) AS Result2
Output

LEFT()
The LEFT function extracts a substring from a string starting from the left with the specified number of characters.
Example
- DECLARE @Name varchar(30)
- SET @Name = 'Jitendra'
- SELECT LEFT(@Name,3) AS LeftPart
Output
LEN()
The LEN function returns the length of the specified string
Example
- DECLARE @Name varchar(30)
- SET @Name = 'Jitendra'
- SELECT LEN(@Name) AS Length
Output



Jignesh KumarPosted Aug 18, 2018, 9:38 PM
Good explanation .