Name
SUBSTRING( )
Synopsis
SUBSTRING(string, position[, length]
) SUBSTRING(string
FROMposition
FORlength
)
This
function returns the characters of a given string, starting from the
left position given. The first character is numbered 1. You can
restrict the length of the string retrieved by specifying a limit.
This function is similar to MID( )
.
SELECT CONCAT(SUBSTRING(soc_sec, 1, 3), '-', SUBSTRING(soc_sec FROM 4 FOR 2), '-', SUBSTRING(soc_sec FROM 6)) AS 'Social Security Nbr.' FROM students LIMIT 1; +----------------------+ | Social Security Nbr. | +----------------------+ | 433-12-3456 | +----------------------+
This example shows the three types of syntax of SUBSTRING()
for reformatting a Social Security number stored without
dashes.
Get MySQL in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.