Function:Returns part of a character, binary, text, or image expression.
Syntax:Substring (expression, start, length)
The substring function in SQL is used to capture part of a column. The function names are not exactly the same in different databases:
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 ).
Given expression |
Return type |
Image |
Varbinary |
Ntext |
Nvarchar |
-
-
CodeExample:
-
-
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 ()