function: returns part of a character, binary, text, or image expression
Syntax: SUBSTRING (expression, start, length)
the substring function in SQL is used to grab a portion of a field data. The name of this function is not exactly the same in different repositories:
Parameters:
Expression a string, binary string, text, image, column, or expression that contains a column. Do not use an expression that contains aggregate functions.
The start integer or an expression that can be implicitly converted to int, specifying the start position of the substring.
-
-
The length integer, or an expression that can be implicitly converted to int, to specify the lengths of substrings.
-
-
return value:
-
-
If expression is a supported character data type, the character data is returned. If expression is a supported binary data type, the 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 type as the given expression (except for the contents shown in the following table).
given an expression |
return type |
Image |
varbinary |
ntext |
nvarchar |
-
-
code example:
-
-
The following example returns the first letter and full last name of each employee in the Employees table:
SELECT SUBSTRING (name,1,1) as Initial, last Name
From Employees
Here is the result set:
Initial ..... Last Name
-------------------------
A................ Funk
M................ Pearson
L........ Calafato
N................ Danner
J................ Lee
S .......... Byham
M................ Sutter
R................ King
A................ Doyle
- Mysql:substr (), SUBSTRING ()
- Oracle:substr ()
- SQL server:substring ()