Get the App
SLTechnology News&Howtos  ›  Database  › 

What is the basic usage of the MySQL MID () function?

Shulou Source: shulou.com Published: 2022-06-01 18:32:14 10月01日 Update

This article is mainly about the basic usage of the MySQL MID () function. If you are interested, let's take a look at this article. I believe it will be of some reference value to you after reading the basic usage of the MySQL MID () function.

In MySQL, the MID () function returns a substring starting at the specified location.

MID () and SUBSTR () are both synonyms for SUBSTRING ().

The basic syntax is as follows:

MID (str,pos,len)

Here, str is the string, pos is the position of the starting substring, and len is an optional parameter that determines the number of characters returned from the starting position.

There are several different ways to use this function, so the complete syntax looks like this:

MID (str,pos) MID (str FROM pos) MID (str,pos,len) MID (str FROM pos FOR len)

Example 1-basic usage

Here is an example of the MID (str,pos) syntax:

SELECT MID ('I drink coffee', 3) Result

Results:

+-| Result | +-+ | drink coffee | +-+

In this case, I take a substring from the string, starting at position 3.

Example 2-using the FROM clause

This time the MID (str FROM pos) syntax is used:

SELECT MID ('I drink coffee' FROM 3) Result

Results:

+-| Result | +-+ | drink coffee | +-+

The result is the same.

In this case, FROM is the standard SQL. Note that this syntax does not use commas.

Example 3-specify length

In this example, I used the MID (str,pos,len) syntax:

SELECT MID ('I drink coffee', 3,5) Result

Results:

+-| Result | +-+ | drink | +-+

Here I specify that the length of the returned substring is 5 characters.

Example 4-specify the length (using the FOR clause)

In this example, I used the MID (str FROM pos FOR len) syntax:

SELECT MID ('I drink coffee' FROM 3 FOR 5) Result

Results:

+-| Result | +-+ | drink | +-+

So this time we use standard SQL to achieve the same result.

Does the above details about the basic usage of the MySQL MID () function help you? If you want to know more about it, you can continue to follow our industry information section.

Tags: Character grammar string result function position example length content clause standard start different same size value usage interest parameter synonym Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia OPPO Reno Microsoft Linux macOS