Get the App
SLTechnology News&Howtos  ›  Database  › 

How to intercept the first few bits of a string by mysql

Shulou Source: shulou.com Published: 2022-05-31 12:44:54 09月14日 Update

This article mainly explains "how to intercept the first few bits of a string by mysql". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how mysql intercepts the first few bits of a string".

Mysql intercepts the first few bits of a string: 1, using the LEFT () function, syntax "left (intercepted string, intercepted length)"; 2, using SUBSTRING () function, syntax "substring (intercepted string, 1, intercepted length)".

The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.

Mysql's method to intercept the first few bits of a string:

1. Use the LEFT () function

The LEFT () function can intercept strings from the left

Usage: left (str, length), that is: left (truncated string, intercepted length)

[example] use the LEFT function to return the left character in the string

Mysql > SELECT LEFT ('MySQL',2); +-+ | LEFT (' MySQL',2) | +-+ | My | +-+ 1 row in set (0.04 sec)

As you can see from the running result, a substring of length 2 starting to the left of the string "MySQL" is returned, and the result is "My".

2. Use the SUBSTRING () function

The SUBSTRING () function intercepts a string of a specific length

Usage:

Substring (str, pos, length), namely: substring (intercepted string, from which bit to intercept, intercept length)

When the second argument of the SUBSTRING () function, pos, is 1, it intercepts a string of a specific length from the beginning.

It is also possible to use a negative value for pos. If so, the position of the substring begins with the pos character at the end of the string, that is, the penultimate pos character, not the beginning of the string.

[example] use the SUBSTRING function to get the substring at the specified location

Mysql > SELECT SUBSTRING ('computer',1,3) AS col1,-> SUBSTRING (' computer',3,4) AS col2,-> SUBSTRING ('computer',-3) AS col3,-> SUBSTRING (' computer',-5,3) AS col4 +-+ | col1 | col2 | col3 | col4 | +-+ | com | mput | ter | put | +-+ 1 row in set (0.00 sec) so far I believe you have a deeper understanding of "mysql how to intercept the first few bits of a string". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Character string function length position method content example beginning result grammar learning practical deeper interest parameter practicality practicality simple operation tutorial Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB macOS Linux Shulou Tech Info Huawei