String Functions

MySQL contains a considerable number of string functions. They are listed in Appendix C. We will consider a small subset of them here to give you a flavor of what you can do. Here is the syntax of some of the more popular functions:

  • LEFT(string, num) and RIGHT(string, num)

  • LENGTH(string)

  • SUBSTRING(string, position [,length])

  • CONCAT(string1, string2, ...)

LEFT() and RIGHT() return the leftmost or rightmost num characters. The return will be an empty string if num is less than 1. Thus:

SELECT LEFT("abcde", 4) returns abcd.

SELECT RIGHT("abcde", 1) returns e.

LENGTH() determines the length of a string, so

SELECT LENGTH("abcde") returns 5.

SUBSTRING() allows you to extract from character position in string and optionally specify a

Get Sams Teach Yourself MySQL in 21 Days, Second Edition 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.