Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

String functions

From: MySQL Essential Training

Video: String functions

MySQL provides a wide variety of string functions for use in your queries. And this'll simply take those two columns and jam them together like this.

String functions

MySQL provides a wide variety of string functions for use in your queries. I'll cover the major ones here. In this lesson, we'll be using the world database. We'll start with a simple query that get's the length of the LocalName from the Country table. And so we're going to SELECT and we'll get both Name and LocalName. But we're just going to take the length of the LocalName, using the LENGTH function. And we're selecting just for WHERE Continent equals Europe, so we have a manageable list of countries.

And we'll order it by that length and you'll notice that I gave the LENGTH an alias, using the AS clause. I'm using that AS clause for the ORDER BY. So, we don't have to use the whole LENGTH function twice in the query. We'll order descending, so that the larger ones are at the top. And I'll press Go. And here we have our lengths. And so you can see the LocalName here, and the Length of the local name. And here's something interesting to notice.

Let's get down to some of the smaller ones. You see the ones with accented characters. This one here has six letters, but it shows the Length as 7. And that discrepancy is because the accented letter actually takes up two bytes in the Unicode character set. This is UTF8. And so some of the accent letters take up 2 bytes rather than just 1, and so the length of Espania even though it's six letters shows up as 7, because the length function is actually counting the bytes rather than the characters.

So there's another version of the length function called CHAR_LENGTH, CHAR_LENGTH. And if we use that instead, you notice that our result is different. We get down here to some of the smaller ones again. Here you can see Espania, which was 7 before is now 6. And Romania shows as 7 instead of 8. And that's because now we're counting the characters rather than the bytes. Of course the character length function will take a little bit longer because it has to parse out the Unicode and compare it to the table and all.

Whereas the LENGTH function can just count bytes. But if you're really concerned with length and character then character length is the function that you'll want to use. TheY're also string functions for subsets, and take out this ORDER BY because we're not going to be using the LENGTH anymore, and now we'll SELECT Name, and some subsets of name we'll say LEFT. And that will get the left three characters from the Name column. And we can do the same thing with RIGHT.

And there's one called MID for getting substrings from the middle of a string. And this takes two parameters, so we'll say Name comma 2 comma 3. And that will get the three characters starting at the second character position. And, so when I say Go here, you see that, here we have the left three letters, the right three letters. So starting from the right, nia, and the middle three, starting at the second column. So the lba, or in this case, ndo.

So it's very simple, these are simple ways to get substrings from strings using LEFT, RIGHT and MID. There's also concatenation functions, we can concatenate, Name and LocalName. And this'll simply take those two columns and jam them together like this. So if you wanted to put something in between them using concatenate, you would have to, or we can say comma space, and you would concatenate that literal string along with it, and that would work fine.

Or you can say concatenate with separator, WS. And we can take that separator and put it as the first parameter. And that way, we could also concatenate some other things if we wanted to say, Region and Continent. And those will all be concatenated with the separator, comma, space, or we could use a slash like this. And so you can concatenate with or without the separators.

CONCAT_WS is the version with separators. There's also a LOCATE function. And start this just to show you how it works without a query from a table. We can say LOCATE bar in foobarbaz. And it will give us the character position so, one, two, three, four. It should return a 4. And there's our 4. So it says that the string bar begins at the fourth position within that other string foobarbaz.

And of course you can use this in a query from a table. So you can say, LOCATE stan, and we'll give it a name here first, so we can see the whole name. From Name column FROM Country WHERE Continent equals Europe. Or rather, WHERE Name LIKE. And it has the string, stan, in it. And so there's our countries with stan in them. You notice it's towards the end of all of them. But we have that.

Starts at the eighth position in Afghanistan the sixth position in Kazakhstan or we can simply locate a single letter if we want to. Okay, I'll put my K in the LIKE as well. And these are all the countries with K's in them, we got 26 rows. So the LOCATE function is great for locating a string within another string. There's also simple functions for uppercase and lowercase, and they also work for Unicode. And you'll notice that this lowercase e with the accent over it, the double dots, the umlaut, comes up in uppercase here.

And there's another lowercase e like that, and some of these other ones here. There's the slashed o. And likewise, there's one for lowercase, so I can type LOWER here and LOWER here. And it does the same thing, except it makes them lowercase. And you'll notice this capital O and Oosterlicht is made into a lowercase o. And finally there's a function for reversing character strings. And that also works with Unicode. Now you see our strings are reversed.

So MySQL provides a rich set of string function. These are just the most commonly used. You can see the MySQL documentation for the rest of them.

Show transcript

This video is part of

Image for MySQL Essential Training
MySQL Essential Training

60 video lessons · 5799 viewers

Bill Weinman

Expand all | Collapse all
  1. 4m 22s
    1. Welcome
      1m 3s
    2. Using the exercise files
      1m 31s
    3. What is MySQL?
      1m 48s
  2. 45m 37s
    1. Installation overview
      3m 16s
    2. Installing XAMPP on Windows
      5m 55s
    3. Installing XAMPP on the Mac
      6m 38s
    4. Setting up MySQL users
      11m 31s
    5. Installing SID on Windows
      5m 43s
    6. Installing SID on the Mac
      6m 6s
    7. Installing time zone support in MySQL on Windows
      6m 28s
  3. 45m 43s
    1. The SELECT statement
      3m 57s
    2. Selecting rows
      4m 57s
    3. Selecting columns
      3m 8s
    4. Sorting results with ORDER BY
      2m 58s
    5. Filtering results with WHERE
      3m 52s
    6. Filtering results with LIKE and IN
      3m 41s
    7. Filtering results with regular expressions
      8m 21s
    8. Inserting rows
      4m 9s
    9. Updating rows
      2m 21s
    10. Deleting rows
      2m 25s
    11. Literal strings
      3m 12s
    12. Understanding NULL
      2m 42s
  4. 41m 47s
    1. Creating a database
      4m 30s
    2. Creating a table
      7m 18s
    3. Creating indexes
      6m 8s
    4. Controlling column behavior with constraints
      4m 46s
    5. Creating an ID column
      6m 58s
    6. Using foreign key constraints
      7m 58s
    7. Altering a table
      4m 9s
  5. 28m 56s
    1. What are data types?
      4m 1s
    2. Numeric types
      5m 21s
    3. String types
      2m 58s
    4. Date and time types
      7m 2s
    5. Bit type
      2m 26s
    6. Boolean values
      2m 15s
    7. Enumeration types
      4m 53s
  6. 32m 34s
    1. String functions
      6m 57s
    2. Numeric functions
      6m 2s
    3. Date and time functions
      4m 12s
    4. Time zones in MySQL
      3m 37s
    5. Formatting dates
      1m 51s
    6. Aggregate functions
      5m 45s
    7. Flow control with CASE
      4m 10s
  7. 7m 6s
    1. Maintaining database integrity with transactions
      4m 46s
    2. Using transactions for performance
      2m 20s
  8. 16m 49s
    1. Updating a table with a trigger
      5m 11s
    2. Preventing automatic updates with a trigger
      7m 29s
    3. Logging transactions with a trigger
      4m 9s
  9. 14m 11s
    1. Creating a simple subselect
      3m 23s
    2. Searching within a result set
      3m 53s
    3. Creating a view
      3m 32s
    4. Creating a joined view
      3m 23s
  10. 12m 26s
    1. Understanding MySQL stored routines
      2m 0s
    2. Creating a stored function
      4m 34s
    3. Creating a stored procedure
      5m 52s
  11. 14m 4s
    1. The multi-platform PDO interface
      3m 44s
    2. Executing the SQL
      4m 8s
    3. Implementing auto-increment IDs
      2m 3s
    4. Using a stored funciton
      4m 9s
  12. 1m 3s
    1. Goodbye
      1m 3s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.

Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.

Mark all as unwatched Cancel


You have completed MySQL Essential Training.

Return to your organization's learning portal to continue training, or close this page.

Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?


Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.