Get the App
SLTechnology News&Howtos  ›  Database  › 

How to use SQL statements to intercept strings in MySQL

Shulou Source: shulou.com Published: 2022-05-31 22:56:08 11月12日 Update

This article will explain in detail how to use SQL statements to intercept strings in MySQL. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

1. Left (name,4) intercepts the four characters on the left

Column:

SELECT LEFT (201809 / 04)

Results: 2018

2. Right (name,2) intercepts the 2 characters on the right

SELECT RIGHT (February, 201809) month

Results: 09

3. SUBSTRING (name,5,3) intercepts name. This field only intercepts the following 3 characters from the fifth character.

SELECT SUBSTRING ('Chengdu financing Department', 5Pol 3)

Result: business department

4. SUBSTRING (name,3) intercepts the name field from the third character, followed by all the characters

SELECT SUBSTRING (Chengdu financing Department, 3)

Result: financing Division

5. SUBSTRING (name,-4) intercepts the fourth character position (reciprocal) of the name field from beginning to end.

SELECT SUBSTRING (Chengdu financing Department,-4)

Result: capital business department

6. SUBSTRING (name,-4Power2) intercepts the fourth character position (reciprocal) of the name field, and only intercepts the next two characters.

SELECT SUBSTRING ('Chengdu financing Department',-4jin2)

Result: capital management

Note: we notice that in the function substring (str,pos, len), pos can be negative, but len cannot be negative.

7. Substring_index ('www.baidu.com','.', 2) intercepts the second'.' All previous characters

SELECT substring_index ('www.baidu.com','. 2)

Results: www.baidu

8. Substring_index ('www.baidu.com','.',-2) intercepts all characters after the second'.'(reciprocal)

SELECT substring_index ('www.baidu.com','.'- 2)

Results: baidu.com

9. SUBSTR (name, 1, CHAR_LENGTH (name)-3) intercepts the name field and removes all the characters in the last three digits of the name field

SELECT SUBSTR (Chengdu financing Department, 1, CHAR_LENGTH (Chengdu financing Department)-3)

Result: Chengdu financing

On how to use SQL statements in MySQL to intercept strings to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Characters business business results financing Chengdu fields strings statements locations content articles more knowledge articles negative values good three functions right Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn OPPO Reno Shulou Technology MariaDB Docker