MySQL 常见str函数

MySQL常见的字符串函数

整理自官档。

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

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

时间: 2024-08-24 19:35:59

MySQL 常见str函数的相关文章

常见string函数(python)

常见str函数补充 str.upper()//全部变大写 str.bit_length()//求长度 str.capitalize()#首字母大写 str.casefold() str.lower()#以上两个均小写 str.center(20,'#')#20格居中,以#填充 str.ljust(20,'#')# str.rjust(20,'#')#左右填充,上同 str.zfill()#0填充 str.strip()#用来删除空格or指定字符(left or right) str.count(

MYSQL 常见的内置函数与自定义函数

MySQL 内置函数: 字符函数 数值函数 时间日期函数 常见的数值函数的使用: 1 select avg(tdb_goods) from tdb_goods; //求字段值的平均数 内置的求和函数: 1 select sum(goods_price) from tdb_goods; //求字段值的和 常见的日期函数举例 1 select now(); 1 select current_timestamp(); 用户自定义函数: 语法 20.2.1. CREATE PROCEDURE和CREAT

MYSQL常见运算符和函数

MYSQL常见运算符和函数[重要] 字符函数 (1)CONCAT():字符连接 SELECT CONCAT('IMOOC','-','MySQL');//IMOOC-MySQL SELECT CONCAT (first_name,last_name) AS fullname FROM test; (2)CONCAT_WS():使用指定的分隔符进行字符连接,(第一个位置指定分隔符,后面的为分割的内容) SELECT CONCAT_WS('%','abc','def'); //第一个是指定的分隔符:

Mysql中的函数

阅读目录 什么是函数 与存储过程的区别 mysql自带函数 自定义函数 什么是函数 mysql中的函数与存储过程类似,都是一组SQL集: 与存储过程的区别 函数可以return值,存储过程不能直接return,但是有输出参数可以输出多个返回值: 函数可以嵌入到sql语句中使用,而存储过程不能: 函数一般用于实现较简单的有针对性的功能(如求绝对值.返回当前时间等),存储过程用于实现复杂的功能(如复杂的业务逻辑功能): mysql自带函数 mysql本身已经实现了一些常见的函数,如数学函数.字符串函

Mysql处理字符串函数(转)

http://www.jb51.net/article/27458.htm 感觉上MySQL的字符串函数截取字符,比用程序截取(如PHP或JAVA)来得强大,所以在这里做一个记录,希望对大家有用. 函数: 1.从左开始截取字符串 left(str, length) 说明:left(被截取字段,截取长度) 例:select left(content,200) as abstract from my_content_t 2.从右开始截取字符串 right(str, length) 说明:right(

MySQL常见注意事项及优化

MySQL常见注意事项 模糊查询 like 默认是对name字段建立了索引 注意:在使用模糊查询的时候,当% 在第一个字母的位置的时候,这个时候索引是无法被使用的.但是% 在其他的位置的时候,索引是可以被使用的. ? # select * from tableName where name like "%zhangsan"; ?可以使用到索引啊? 不可以. 分析:因为是不确定查询,在表中任何一行记录都有可能满足查询条件. ? #select * from tableName where

MYSQL \ PHP日期函数互相转换

MySQL 获得当前日期时间 函数 来源:http://www.cnblogs.com/ggjucheng/p/3352280.html 获得当前日期+时间(date + time)函数:now() mysql> select now(); +---------------------+ | now() | +---------------------+ | 2008-08-08 22:20:46 | +---------------------+ MySQL 获得当前时间戳函数:current

MySQL常用字符函数简介

<html> <body> <h1>MySQL常用字符函数简介</h1> <table>     <tr>         <td>CONCAT(S1,S2...Sn)</td>         <td>连接S1,S2...Sn为一个字符串</td>     </tr> </table> <p style="background-color:yel

mysql(常用函数)转

对于针对字符串位置的操作,第一个位置被标记为1. ASCII(str) 返回字符串str的最左面字符的ASCII代码值.如果str是空字符串,返回0.如果str是NULL,返回NULL. mysql> select ASCII('2');    -> 50mysql> select ASCII(2);    -> 50mysql> select ASCII('dx');    -> 100也可参见ORD()函数. ORD(str) 如果字符串str最左面字符是一个多字节