MySQL 常见str函数
admin
2023-05-17 22:22:17
0

MySQL常见的字符串函数

整理自官档。

1.1     SUBSTR or SUBSTRING

SUBSTR(str,pos), SUBSTR(str FROM pos),SUBSTR(str,pos,len), SUBSTR(str FROM

pos FOR len)

SUBSTR() is a synonym for SUBSTRING().

SUBSTRING(str,pos), SUBSTRING(str FROM pos),SUBSTRING(str,pos,len),

SUBSTRING(str FROM pos FOR len)

The forms without a len argument return asubstring from string str starting at position pos.

The forms with a len argument return asubstring len characters long from string str, starting at

position pos. The forms that use FROM arestandard SQL syntax. It is also possible to use a negative

value for pos. In this case, the beginningof the substring is pos characters from the end of the

string, rather than the beginning. Anegative value may be used for pos in any of the forms of this

function.

For all forms of SUBSTRING(), the positionof the first character in the string from which the

substring is to be extracted is reckoned as1.

mysql> SELECTSUBSTRING('Quadratically',5);

-> 'ratically'

mysql> SELECT SUBSTRING('foobarbar' FROM4);

-> 'barbar'

mysql> SELECTSUBSTRING('Quadratically',5,6);

-> 'ratica'

mysql> SELECT SUBSTRING('Sakila', -3);

-> 'ila'

mysql> SELECT SUBSTRING('Sakila', -5,3);

-> 'aki'

mysql> SELECT SUBSTRING('Sakila' FROM -4FOR 2);

-> 'ki'

This function is multibyte safe.

If len is less than 1,the result is the empty string.

1.2     SUBSTRING_INDEX

SUBSTRING_INDEX(str,delim,count)

Returns the substring from string strbefore count occurrences of the delimiter delim. If count

is positive, everything to the left of thefinal delimiter (counting from the left) is returned. If count

is negative, everything to the right of thefinal delimiter (counting from the right) is returned.

SUBSTRING_INDEX() performs a case-sensitivematch when searching for delim.

mysql> SELECTSUBSTRING_INDEX('www.mysql.com', '.', 2);

-> 'www.mysql'

mysql> SELECTSUBSTRING_INDEX('www.mysql.com', '.', -2);

-> 'mysql.com'

This function is multibyte safe.

1.3     CHAR_LENGTH

CHAR_LENGTH(str) 返回字符数

Returns the length of the string str,measured in characters. A multibyte character counts as a

single character. This means that for astring containing five 2-byte characters, LENGTH() returns

10, whereas CHAR_LENGTH() returns 5.

1.4     LENGTH

LENGTH(str) 返回字节数

Returns the length of the string str,measured in bytes. A multibyte character counts as multiple

bytes. This means that for a stringcontaining five 2-byte characters, LENGTH() returns 10, whereas

CHAR_LENGTH() returns 5.

mysql> SELECT LENGTH('text');


相关内容

热门资讯

500左右的手柄怎么选?swi... 现在游戏手柄早已成为热门数码外设,跟风入手的玩家数不胜数,但不清楚500左右的手柄怎么选的人同样不在...
原创 美... 2026年1月1日,美国太空探索技术公司“星链”工程副总裁迈克尔尼科尔斯高调宣布,计划2026年降低...
原创 全... 现在的屏幕设计,拥有两大类型,分别是折叠屏、直板机,而类型之间拥有多种形态。其中的折叠屏,拥有阔折叠...
原创 流... 最近,通信圈的两则消息引发了轩然大波。一方面,中国电信宣布自8月1日起全面停止第三方互联网渠道的办卡...
伊朗重申霍尔木兹海峡不会恢复至... △伊朗外交部发言人巴加埃(资料图)当地时间2日,伊朗外交部发言人巴加埃表示,伊方重申,霍尔木兹海峡的...
伊朗外长:与阿曼谈判接近完成 △伊朗外交部长阿拉格齐(中)当地时间8月2日,伊朗外交部长阿拉格齐在政府内阁会议上介绍伊朗与阿曼谈判...
河南2026年“三支一扶”计划... 针对近期备受关注的2026年河南省“三支一扶”计划招募相关舆情,河南省“三支一扶”领导小组协调办公室...
以卵击石的“汉光演习”不过是一... 昨天题为《人民解放军越强大,台海和平越有保障》的海风文章,已经反映了一部分台海局势的现实。若再把8月...
美国一养老社区将推出面向老人的... 据《独立报》8月1日报道,美国亚利桑那州一养老社区将引入一台专为老年人设计的大麻自助售卖机,居民无需...
“你头发都白了”,同守广西边防... “你头发都白了,模样还是没变,就是没有年轻时候那么帅了。”