MySQL字符串函数
字符串大写和小写转换
MySQL 字符串大写和小写转化函数有两对: lower(), uppper() 和 lcase(), ucase()
mysql> select lower('DDD');
+--------------+
| lower('DDD') |
+--------------+
| ddd |
+--------------+
mysql> select upper('ddd');
+--------------+
| upper('ddd') |
+--------------+
| DDD |
+--------------+
mysql> select lcase('DDD');
+--------------+
| lcase('DDD') |
+--------------+
| ddd |
+--------------+
mysql> select ucase('ddd');
+--------------+
| ucase('ddd') |
+--------------+
| DDD |
+--------------+
通常情况下,我选择 lower(), upper() 来转换字符串大写和小写。由于这和其它数据库中函数相兼容。
清除字符串首尾空格
MySQL 中的清除字符串首尾空格函数有三个: ltrim(), rtrim(), trim()
mysql> select concat('.', ltrim(' ddd '), '.');
+----------------------------------+
| concat('.', ltrim(' ddd '), '.') |
+----------------------------------+
| .ddd . |
+----------------------------------+
mysql> select concat('.', rtrim(' ddd '), '.');
+----------------------------------+
| concat('.', rtrim(' ddd '), '.') |
+----------------------------------+
| . ddd. |
+----------------------------------+
mysql> select concat('.', trim(' ddd '), '.');
+---------------------------------+
| concat('.', trim(' ddd '), '.') |
+---------------------------------+
| .ddd. |
+---------------------------------+
MySQL 中的 trim 字符串函数,实在是强大。它不仅能消除字符串首尾部的空格,还能够消除我们指定的随意字符。
ltrim(), rtrim() 仅仅是它的一个功能子集。来看下 trim 函数的完整语法:
1. trim([{both | leading | trailing} [remstr] from] str)
2. trim([remstr from] str)
1. 清除字符串首部字符。
mysql> select trim(leading '.' from '..ddd..');
+----------------------------------+
| trim(leading '.' from '..ddd..') |
+----------------------------------+
| ddd.. |
+----------------------------------+
2. 清除字符串尾部字符。
mysql> select trim(trailing '.' from '..ddd..');
+-----------------------------------+
| trim(trailing '.' from '..ddd..') |
+-----------------------------------+
| ..ddd |
+-----------------------------------+
3. 清除字符串首尾部字符。
mysql> select trim(both '.' from '..ddd..');
+-------------------------------+
| trim(both '.' from '..ddd..') |
+-------------------------------+
| ddd |
+-------------------------------+ mysql> select trim('.' from '..ddd..');
+--------------------------+
| trim('.' from '..ddd..') |
+--------------------------+
| ddd |
+--------------------------+
trim() 默认清除字符串首尾部的空格。
字符串截取
MySQL 字符串截取函数:left(), right(), substring(), substring_index()。还有 mid(), substr()。当中,mid(), substr() 等价于 substring() 函数,substring() 的功能很强大和灵活。
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字符串函数.日期时间函数 一.常见字符串函数: 1.CHAR_LENGTH 获取长度(字符为单位) 2.FORMAT 格式化 3.INSERT 替换的方式插入 4.INSTR 获取位 ...
- MySQL字符串函数substring:字符串截取
MySQL 字符串截取函数:left(), right(), substring(), substring_index().还有 mid(), substr().其中,mid(), substr() ...
- mysql字符串函数(转载)
对于针对字符串位置的操作,第一个位置被标记为1. ASCII(str) 返回字符串str的 最左面字符的ASCII代码值.如果str是空字符串, 返回0.如果str是NULL,返回NULL. mysq ...
- 【转】mysql字符串函数
对于针对字符串位置的操作,第一个位置被标记为1(即:第一个字母索引为1). ASCII(str) 返回字符串str的 最左面字符的ASCII代码值.如果str是空字符串, 返回0.如果str是NULL ...
- mysql 字符串函数
对于针对字符串位置的操作,第一个位置被标记为1. ASCII(str) 返回字符串str的 最左面字符的ASCII代码值.如果str是空字符串, 返回0.如果str是NULL,返回NULL. mysq ...
- MySQL字符串函数:字符串截取
MySQL 字符串截取函数:left(), right(), substring(), substring_index().还有 mid(), substr().其中,mid(), substr() ...
- Mysql 字符串函数 详解
字符串函数是最常用的一种函数了,如果大家编写过程序的话,不妨回过头去看看自己使用过的函数,可能会惊讶地发现字符串处理的相关函数占已使用过的函数很大一部分.MySQL中字符串函数也是最丰富的一类函数,表 ...
- MySQL数据库学习笔记(五)----MySQL字符串函数、日期时间函数
一.常见字符串函数: 1.CHAR_LENGTH 获取长度(字符为单位) 2.FORMAT 格式化 3.INSERT 替换的方式插入 4.INSTR 获取位置 5.LEFT/RIGHT 取左 ...
- mysql 字符串函数、分组函数
字符串函数 1.concat 函数 drop table test;create table test(id int(4), name varchar(10), sex char(2));insert ...
随机推荐
- Firebug中命令行栏(Commandlinie)的使用介绍和总结
Commandlinie是Firebug中总有用的一个特性.如果你有Microsoft Visual Studio的使用经验,你就会知道“Immediate Window” 和“Watch Windo ...
- POJ 2049 Finding Nemo bfs 建图很难。。
Finding Nemo Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 6952 Accepted: 1584 Desc ...
- 302重定向,MVC中的Get,Post请求。
1.在访问页遇到重定向,Get,Post跳转处理,在跳转后的页面获取访问端的IP,他们的IP是否发生变化... 2.重定向处理后获取的IP还是访问端IP,而用Get,Post请求处理后,获取的访问端I ...
- iOS开发(Objective-C)常用库索引
code4app.com 这网站不错,收集各种 iOS App 开发可以用到的代码示例 cocoacontrols.com/ 英文版本的lib收集 objclibs.com/ 精品lib的收集网站 h ...
- Spring整合CXF,发布RSETful 风格WebService
原文地址:http://www.cnblogs.com/hoojo/archive/2012/07/23/2605219.html 这篇文章是承接之前CXF整合Spring的这个项目示例的延伸,所以有 ...
- spring 动态数据源
1.动态数据源: 在一个项目中,有时候需要用到多个数据库,比如读写分离,数据库的分布式存储等等,这时我们要在项目中配置多个数据库. 2.原理: (1).spring 单数据源获取数据连接过程: ...
- jstat命令(Java Virtual Machine Statistics Monitoring Tool)
1.介绍 Jstat用于监控基于HotSpot的JVM,对其堆的使用情况进行实时的命令行的统计,使用jstat我们可以对指定的JVM做如下监控: - 类的加载及卸载情况 - 查看新生代.老生代及持久代 ...
- SparkSQL配置和使用初探
1.环境 OS:Red Hat Enterprise Linux Server release 6.4 (Santiago) Hadoop:Hadoop 2.4.1 Hive:0.11.0 JDK:1 ...
- Java静态类
先要澄清和区别一些概念,“静态类”和“所有方法皆为静态方法的类”. 严格说来,Java中的静态类,指的是“static class”这样修饰的类定义,语法上的要求,使得这样的类一定是内部类,换言之,“ ...
- Volley框架支持HTTPS请求。
第一次写帖子,嘿嘿. 最近了解到google2013IO大会出了个网络框架,正好项目也需要用到,就看了下. 最后发现接口都是HTTPS的,但是Volley默认是不支持HTTPS,网上找了好久,都没有对 ...