How do I use substring to extract a string in MySQL?

In MySQL, you can use the SUBSTRING function to extract a portion of a string.
Its syntax is as follows:

Return a portion of the string ‘str’ starting from the position ‘start’ and with a length of ‘length’.

Among them,

  1. str: string to be intercepted.
  2. Start: The initial position (counting from 1).
  3. Length: the desired amount to be cut.

Here are a few examples:

  1. Get the first 5 characters of a string.

Get the first five characters from the string ‘Hello World’; The output will be “Hello”.

  1. Extract the characters from the 6th position to the end of the string.

Select the characters starting from position 6 in the string ‘Hello World’.
– Output: World

  1. Retrieve the last 5 characters of the string.

– Output: World

  1. Extract characters from the 2nd to the 7th position of the string:

– Result: ello W

Note: If either start or length is negative, it indicates counting from the end of the string.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds