MySQL字符串函数:字符串截取
2012-03-19 09:39
513 查看
MySQL 字符串截取函数:left(), right(), substring(), substring_index()。还有 mid(), substr()。其中,mid(), substr() 等价于 substring() 函数,substring() 的功能非常强大和灵活。
1. 字符串截取:left(str, length)
2. 字符串截取:right(str, length)
3. 字符串截取:substring(str, pos); substring(str, pos, len)
3.1 从字符串的第 4 个字符位置开始取,直到结束。
3.2 从字符串的第 4 个字符位置开始取,只取 2 个字符。
3.3 从字符串的第 4 个字符位置(倒数)开始取,直到结束。
3.4 从字符串的第 4 个字符位置(倒数)开始取,只取 2 个字符。
我们注意到在函数 substring(str,pos, len)中, pos 可以是负值,但 len 不能取负值。
4. 字符串截取:substring_index(str,delim,count)
4.1 截取第二个 '.' 之前的所有字符。
4.2 截取第二个 '.' (倒数)之后的所有字符。
4.3 如果在字符串中找不到 delim 参数指定的值,就返回整个字符串
1. 字符串截取:left(str, length)
mysql> select left('sqlstudy.com', 3); +-------------------------+ | left('sqlstudy.com', 3) | +-------------------------+ | sql | +-------------------------+
2. 字符串截取:right(str, length)
mysql> select right('sqlstudy.com', 3); +--------------------------+ | right('sqlstudy.com', 3) | +--------------------------+ | com | +--------------------------+
3. 字符串截取:substring(str, pos); substring(str, pos, len)
3.1 从字符串的第 4 个字符位置开始取,直到结束。
mysql> select substring('sqlstudy.com', 4); +------------------------------+ | substring('sqlstudy.com', 4) | +------------------------------+ | study.com | +------------------------------+
3.2 从字符串的第 4 个字符位置开始取,只取 2 个字符。
mysql> select substring('sqlstudy.com', 4, 2); +---------------------------------+ | substring('sqlstudy.com', 4, 2) | +---------------------------------+ | st | +---------------------------------+
3.3 从字符串的第 4 个字符位置(倒数)开始取,直到结束。
mysql> select substring('sqlstudy.com', -4); +-------------------------------+ | substring('sqlstudy.com', -4) | +-------------------------------+ | .com | +-------------------------------+
3.4 从字符串的第 4 个字符位置(倒数)开始取,只取 2 个字符。
mysql> select substring('sqlstudy.com', -4, 2); +----------------------------------+ | substring('sqlstudy.com', -4, 2) | +----------------------------------+ | .c | +----------------------------------+
我们注意到在函数 substring(str,pos, len)中, pos 可以是负值,但 len 不能取负值。
4. 字符串截取:substring_index(str,delim,count)
4.1 截取第二个 '.' 之前的所有字符。
mysql> select substring_index('www.sqlstudy.com.cn', '.', 2); +------------------------------------------------+ | substring_index('www.sqlstudy.com.cn', '.', 2) | +------------------------------------------------+ | www.sqlstudy | +------------------------------------------------+
4.2 截取第二个 '.' (倒数)之后的所有字符。
mysql> select substring_index('www.sqlstudy.com.cn', '.', -2); +-------------------------------------------------+ | substring_index('www.sqlstudy.com.cn', '.', -2) | +-------------------------------------------------+ | com.cn | +-------------------------------------------------+
4.3 如果在字符串中找不到 delim 参数指定的值,就返回整个字符串
mysql> select substring_index('www.sqlstudy.com.cn', '.coc', 1); +---------------------------------------------------+ | substring_index('www.sqlstudy.com.cn', '.coc', 1) | +---------------------------------------------------+ | www.sqlstudy.com.cn | +---------------------------------------------------+
相关文章推荐
- MySQL字符串函数:字符串截取
- MySQL 字符串函数:字符串截取
- MySQL 字符串函数:字符串截取
- MySQL字符串函数:字符串截取
- MySQL字符串函数使用样例(字符串截取)
- MySQL 字符串函数:字符串截取
- MySQL字符串函数:字符串截取
- MySQL 字符串函数:字符串截取
- MySQL 字符串函数:字符串截取
- MySQL 字符串函数:字符串截取
- MySQL 字符串函数:字符串截取
- MySQL 字符串函数:字符串截取
- MySQL字符串函数:字符串截取
- MySQL 字符串函数:字符串截取
- Mysql字符串截取函数SUBSTRING的用法说明
- MySQL的字符串函数截取字符
- 关于mysql根据特定字符来截取字符串的一些方法
- mysql 字符串函数(查询处理字符串)
- mysql中字符串的截取函数SUBSTRING_INDEX
- MySQL 中文截取 字符串 中英文混合截取 无乱码