mysql字符串函数之substr,substring用法

语法

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

SUBSTR() is a synonym for SUBSTRING().

SUBSTR()是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 a substring from string str starting at position pos. The forms with a len argument return a substring len characters long from string str, starting at position pos. The forms that use FROM are standard SQL syntax. It is also possible to use a negative value for pos. In this case, the beginning of the substring is pos characters from the end of the string, rather than the beginning. A negative value may be used for pos in any of the forms of this function.
For all forms of SUBSTRING(), the position of the first character in the string from which the substring is to be extracted is reckoned as 1.

没有len参数的形式是字符串str从位置pos开始返回一个子字符串。 带有len参数的形式是字符串str从位置pos开始返回长度为len的子字符串。 使用FROM的形式是标准的SQL语法。 也可以对pos使用负值。 在这种情况下,子字符串的开头是字符串末尾的pos字符,而不是开头。 在这个函数的任何形式中pos可以使用负值。
对于所有形式的SUBSTRING(),从中提取子串的字符串中第一个字符的位置被认为是1。



实例

SELECT substring('chinese', 3);               # 第三个字符之后的子字符串-inese
SELECT substring('chinese', -3);              # 倒数第三个字符之后的子字符串-ese
SELECT substring('chinese', 3, 2);            # 第三个字符之后的两个字符-in
SELECT substring('chinese', -3, 2);           # 倒数第三个字符之后的两个字符-es

SELECT substring('chinese' FROM 3);           # 第三个字符之后的子字符串-inese
SELECT substring('chinese' FROM -3);          # 倒数第三个字符之后的子字符串-ese
SELECT substring('chinese' FROM 3 FOR 2);     # 第三个字符之后的两个字符-in
SELECT substring('chinese' FROM -3 FOR 2);    # 倒数第三个字符之后的两个字符-es

你可能感兴趣的:(【mysql】)