字符串大写和小写转换

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字符串函数的更多相关文章

  1. MySQL字符串函数、日期时间函数

    MySQL字符串函数.日期时间函数 一.常见字符串函数: 1.CHAR_LENGTH  获取长度(字符为单位) 2.FORMAT  格式化 3.INSERT  替换的方式插入 4.INSTR  获取位 ...

  2. MySQL字符串函数substring:字符串截取

    MySQL 字符串截取函数:left(), right(), substring(), substring_index().还有 mid(), substr().其中,mid(), substr() ...

  3. mysql字符串函数(转载)

    对于针对字符串位置的操作,第一个位置被标记为1. ASCII(str) 返回字符串str的 最左面字符的ASCII代码值.如果str是空字符串, 返回0.如果str是NULL,返回NULL. mysq ...

  4. 【转】mysql字符串函数

    对于针对字符串位置的操作,第一个位置被标记为1(即:第一个字母索引为1). ASCII(str) 返回字符串str的 最左面字符的ASCII代码值.如果str是空字符串, 返回0.如果str是NULL ...

  5. mysql 字符串函数

    对于针对字符串位置的操作,第一个位置被标记为1. ASCII(str) 返回字符串str的 最左面字符的ASCII代码值.如果str是空字符串, 返回0.如果str是NULL,返回NULL. mysq ...

  6. MySQL字符串函数:字符串截取

    MySQL 字符串截取函数:left(), right(), substring(), substring_index().还有 mid(), substr().其中,mid(), substr() ...

  7. Mysql 字符串函数 详解

    字符串函数是最常用的一种函数了,如果大家编写过程序的话,不妨回过头去看看自己使用过的函数,可能会惊讶地发现字符串处理的相关函数占已使用过的函数很大一部分.MySQL中字符串函数也是最丰富的一类函数,表 ...

  8. MySQL数据库学习笔记(五)----MySQL字符串函数、日期时间函数

    一.常见字符串函数: 1.CHAR_LENGTH  获取长度(字符为单位) 2.FORMAT  格式化 3.INSERT  替换的方式插入 4.INSTR  获取位置 5.LEFT/RIGHT  取左 ...

  9. mysql 字符串函数、分组函数

    字符串函数 1.concat 函数 drop table test;create table test(id int(4), name varchar(10), sex char(2));insert ...

随机推荐

  1. AS3.0定义变量的访问范围

    在AS3.0中变量的默认访问范围是:internal:包内成员可以访问,包外不可访问.AS2.0默认访问范围是public

  2. python【第十五篇】JavaScript

    大纲 1 简介 2 存在形式 3 放置位置 4 变量 5 注释 6 数据类型 7 时间处理 8 语句和异常 9 函数及其作用域 1.JS简介 JavaScript是世界上最流行的脚本语言,因为你在电脑 ...

  3. (MVC)验证用户是否登录 登录认证

    验证类 using System; using System.Collections.Generic; using System.Linq; using System.Web; using Syste ...

  4. 学习Swift--属性

    属性 属性将值跟特定的类.结构或枚举关联.存储属性存储常量或变量作为实例的一部分,而计算属性计算(不是存储)一个值.计算属性可以用于类.结构体和枚举,存储属性只能用于类和结构体. 存储属性和计算属性通 ...

  5. ZeroBraneStudio之支持远程调试

    打开ZBS后,如果需要远程调试得先开启调试服务器:Project->Start Debugger Server 打开之后就可以编辑文件进行测试了.示例代码如下: local ZBS = 'D:/ ...

  6. "Cannot convert value '0000-00-00' from column 2 to TIMESTAMP"mysql时间转换bug

    今天在项目中遇到这样的一个bug,Cannot convert value '0000-00-00' from column 2 to TIMESTAMP 仔细一查,经过http://blog.csd ...

  7. 使用spm build 批量打包压缩seajs 代码

    一,安装环境 1.安装spm spm工具是基于node(nodejs的服务平台)的,因此我们需要先安装 node 和 npm 下载地址:http://nodejs.org/#download.下载完成 ...

  8. python获取本地ip地址的方法

    #_*_coding:utf8_*_ #以下两种方法可以在ubuntu下或者windows下获得本地的IP地址 import socket # 方法一 localIP = socket.gethost ...

  9. Spring与Oauth2整合示例 spring-oauth-server

    原文地址:http://www.oschina.net/p/spring-oauth-server?fromerr=vpTctDBF

  10. CyclicBarrier的介绍和使用

    转自:http://www.itzhai.com/the-introduction-and-use-of-cyclicbarrier.html 类说明: 一个同步辅助类,它允许一组线程互相等待,直到到 ...