SUBSTRING
extracts part of a string, binary, text, or image value.
The start
and length
parameters are specified as
number of characters for character data types and bytes for binary data types
This example returns part of a string that is 3 characters long.
SELECT SUBSTRING('My SQL Tutorial', 4, 3) AS Part
Part |
---|
SQL |
Syntax for the SUBSTRING function.
SUBSTRING (string, start, length)
string
-- a string value or a column name.
start
-- marks the position where the extraction will begin.
length
-- specifies how many characters to extract from the string.
CUSTOMER |
---|
Id |
FirstName |
LastName |
City |
Country |
Phone |
SELECT SUBSTRING(FirstName, 1, 1) + '. ' +
LastName AS Name,
City, Country
FROM Customer
Name | City | Country |
---|---|---|
M. Anders | Berlin | Germany |
A. Trujillo | México D.F. | Mexico |
A. Moreno | México D.F. | Mexico |
T. Hardy | London | UK |
C. Berglund | Luleå | Sweden |
H. Moos | Mannheim | Germany |
![]() |