Operate strings in MySQL: string interception

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

SELECT LEFT(201809,4) 年

Results: 2018

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

SELECT RIGHT(201809,2) 月份

Result: 09

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

SELECT SUBSTRING('成都融资事业部',5,3)

Result: Division

4. SUBSTRING(name,3) intercepts the name field starting from the third character, and all subsequent characters

SELECT SUBSTRING('成都融资事业部',3)

Result: Financing Division

5. SUBSTRING(name, -4) intercepts the 4th character position (reciprocal) of the name field and starts to fetch until the end

SELECT SUBSTRING('成都融资事业部',-4)

Result: Capital Division

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

SELECT SUBSTRING('成都融资事业部',-4,2)

Result: resources

Note: We noticed 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 all characters before the second '.'

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

Result: www.baidu

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

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

Result: baidu.com

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

SELECT SUBSTR('成都融资事业部', 1, CHAR_LENGTH('成都融资事业部')-3) 

Result: Chengdu Financing

About the difference between LENGTH() and CHAR_LENGTH()

We chat number

Guess you like

Origin blog.csdn.net/HHTNAN/article/details/98182709