str mysql_MySQL 常见str函数

整理自官档。

1.1SUBSTR 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.2SUBSTRING_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.3CHAR_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.4LENGTH

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

本文出自 “90SirDB” 博客,请务必保留此出处http://90sirdb.blog.51cto.com/8713279/1941268

MySQL 常见str函数

标签:mysql   substring   substring_index   length   char_length

本条技术文章来源于互联网,如果无意侵犯您的权益请点击此处反馈版权投诉

本文系统来源:http://90sirdb.blog.51cto.com/8713279/1941268

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值