The substring function in SQL is used to capture part of a column. The name of this function is not exactly the same in different databases.
The substring function in SQL is used to capture part of a column. The name of this function is not exactly the same in different databases.
Function:Returns part of a character, binary, text, or image expression.
Syntax:SUBSTRING (expression, start, length)
Parameters:
Expression string, binary string, text, image, column, or expression that contains a column. Do not use expressions that contain aggregate functions.
The start integer or expression that can be implicitly converted to int, which indicates the start position of the stator string.
-
An integer or expression that can be implicitly converted to an int. It refers to the length of the substring.
-
Return Value:
-
If expression is a supported character data type, character data is returned. If expression is a supported binary data type, binary data is returned. If start = 1, the substring starts with the first character of the expression.
The type of the returned string is the same as that of the given expression (except for the content shown in the following table ).
-
Sample Code:
-
The following example returns the first and last names of each employee in the Employees table:
Select substring (First Name, 1, 1) AS Initial, Last Name
FROM Employees
The following is the result set:
Initial ...... Last Name
-------------------------
A ...... Funk
M... Pearson
L ...... Calafato
N ...... Danner
J... Lee
S... Byham
M... Sutter
R... King
A...
- MySQL: SUBSTR (), SUBSTRING ()
- Oracle: SUBSTR ()
- SQL Server: SUBSTRING ()