MySQL string processing function collation SUBSTRING_INDEX

MySQL string processing function collation SUBSTRING_INDEX

1. Commonly used string interception functions
1. left(@str,2): from the string Start cutting on the left .
2. right(@str,3): from the string Start cutting on the right .
3, MID (@str, 2, 2): from the string Start capturing at any position .
4. SUBSTR(@str,2,2): roughly equivalent to MID.
5. SUBSTRING(@str,2,2): roughly equivalent to MID.
6. substring_index(str,delim,count): Intercept the string according to the specific identifier.

2. Code example
1. Define the test string: @str='People's Republic of China';
2. left(@str,2): 'China'. Cut from the left to the second character.
3. right(@str,3): 'Republic'. Cut from the third character from the right.
4. MID(@str,2,2): 'Chinese'. Start interception from the second character, intercept 2 characters.
5. SUBSTR, SUBSTRING: roughly equivalent to MID.
6. Define the test string: set @strs='China, People, Republic';
7. SELECT SUBSTRING_INDEX(@strs,',','1'): 'China'. Truncate the string before the first comma.
8. SELECT SUBSTRING_INDEX(@strs,',','-1'): 'Republic'. Truncate the string before the last comma.
9、需要截取出“人民” 二字: SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(@strs,',','2'),',','-1') 。
10、 关于9的理解: 先截取第二个逗号前的字符串:“ 中华,人民”;再截取逗号最后一个字符串。
11、 SELECT SUBSTRING_INDEX(@strs,'哈哈','2'):' 中华,人民,共和国' 。对于 标识字符串不存在的,会返回当前字符串。


三、其他字符串处理函数
1、 SELECT concat('a','b','c'):字符串连接,若出现null,则返回null。
2、SELECT LENGTH('a啊'):字符串长度计算。 一个中文字符等于3个长度。
3、SELECT LTRIM(" abc"):删除字符串左边空格。 RTRIM 删除字符串右边空格。
4、SELECT SPACE(10): 生成10个字符串长度空格。
5、SELECT replace("aabbaa","aa","jjj"): 字符串替换。 "aa" 替换为 "jjj" 。
6、 SELECT REPEAT("abc",2): 对字符串"abc",进行2次复制。 "abcabc"
7、SELECT insert("aabbcc",2,2,"ok"): 在指定位置插入指定长度的字符串,替换原有字符串。
8、字符串转数字: SELECT 1+"1"; 或 SELECT "1"+1; 结果是: 2。
9、数字转字符串: SELECT CONCAT(1,"1"); 结果是: "11"。
10、 。。。。


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325762320&siteId=291194637