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');


相关内容

热门资讯

原创 外... 外星翻译官的翻车现场:地球语言真的太难了! 银河系翻译官协会(假想机构)最近发布了一份年度报告,总...
首飞成功!已突破大重量载荷发射... 科技日报记者 付毅飞 实习生 张城辉 记者从蓝箭航天获悉,5月14日11时0分,朱雀二号改进型遥五运...
京东携手MAXHUB共推AI智... PChome消息,近日,京东与智能会议平板领域领军品牌MAXHUB正式签署战略合作协议,标志着双方战...
29个智能气象站启动业务试运行 本报北京5月15日电(记者李红梅)记者从中国气象局获悉:北京密云等29个智能气象站已于近日启动业务试...
广西一载15人车辆坠河,10人... 记者从广西环江毛南族自治县相关方面获悉,据初步了解,5月16日21时30分许,一辆汽车在环江县洛阳镇...
iOS 26.5更新苹果地图两... 苹果地图是iOS 26.5此次更新中获得新功能的应用之一。以下是地图用户需要了解的最新变化。 推荐...
每经热评 | 黄仁勋喝蜜雪冰城... 每经评论员 朱成祥 黄仁勋近期到访北京,被拍到在胡同喝豆汁、吃炸酱面、举着蜜雪冰城饮料。他表情轻松,...
我省3项目被列入工信部先进计算... 本报太原5月15日讯(记者王蕾)近日,工业和信息化部公布了2025年先进计算赋能新质生产力典型应用案...
薛贵:学习的革命——脑科学与人... 1 学习的本质不是知识的装卸, 而是雕刻大脑的“芯片” 耶鲁大学前校长理查德·查尔斯·莱文曾说过:如...
伊朗:已制定管理霍尔木兹海峡指... 新华社德黑兰5月16日电 伊朗伊斯兰议会国家安全和外交政策委员会主席易卜拉欣·阿齐兹16日在社交媒体...