mysql你不知道字符串操作函数
LENGTH()函数可以获得一个字符串的长度,代码如下:
- selectlength('abcdefg'),length('0123456789');
- /*
- +-------------------+----------------------+
- |length('abcdefg')|length('0123456789')|
- +-------------------+----------------------+
- |7|10|
- +-------------------+----------------------+
- 1rowinset(0.00sec)
- */
使用RPAD()或者LPAD()函数可以分别从右边或者左边对字符串进行填补,一般,字符串经常用空格进行填补,就是所谓的补齐,MySQL可以通过给RPAD()和LPAD()函数中指定一个自定义值作为第三个参数对字符串进行填补.
- selectrpad('simaopig',17,''),rpad('simaopig',17,'@'),lpad('simaopig',17,''),lpad('simaopig',17,'@');
- /*
- +------------------------+-------------------------+------------------------+-------------------------+
- |rpad('simaopig',17,'')|rpad('simaopig',17,'@')|lpad('simaopig',17,'')|lpad('simaopig',17,'@')|
- +------------------------+-------------------------+------------------------+-------------------------+
- |NULL|simaopig@@@@@@@@@|NULL|@@@@@@@@@simaopig|
- +------------------------+-------------------------+------------------------+-------------------------+
- 1rowinset(0.00sec)
- */
- selectrpad('simaopig',17,''),rpad('simaopig',17,'@'),lpad('simaopig',17,''),lpad('simaopig',17,'@');
- /*
- +-------------------------+-------------------------+-------------------------+-------------------------+
- |rpad('simaopig',17,'')|rpad('simaopig',17,'@')|lpad('simaopig',17,'')|lpad('simaopig',17,'@')|
- +-------------------------+-------------------------+-------------------------+-------------------------+
- |simaopig|simaopig@@@@@@@@@|simaopig|@@@@@@@@@simaopig|
- +-------------------------+-------------------------+-------------------------+-------------------------+
- 1rowinset(0.00sec)
- */
LTRIM()和RTRIM()函数产生与填补相反的效果,去除字符串的首和尾字符:
- selectltrim('simaopig'),ltrim('simaopig'),rtrim('simaopig'),rtrim('simaopig'),ltrim(rtrim('simaopig'));
- /*
- +----------------------+---------------------------+----------------------+--------------------------+----------------------------------+
- |ltrim('simaopig')|ltrim('simaopig')|rtrim('simaopig')|rtrim('simaopig')|ltrim(rtrim('simaopig'))|
- +----------------------+---------------------------+----------------------+--------------------------+----------------------------------+
- |simaopig|simaopig|simaopig|simaopig|simaopig|
- +----------------------+---------------------------+----------------------+--------------------------+----------------------------------+
- 1rowinset(0.00sec)
- */
即然谈到了"空格",那么把书中的例子再抄一遍,那就是MySQL提供了一个函数,这个函数(SPACE())的功能是只返回空格.
- selectspace(1),space(4),length(space(1)),length(space(4));
- /*
- +----------+----------+------------------+------------------+
- |space(1)|space(4)|length(space(1))|length(space(4))|
- +----------+----------+------------------+------------------+
- |||1|4|
- +----------+----------+------------------+------------------+
- 1rowinset(0.00sec)
- */
TRIM()函数可以指定去除格式,并且可以指定是去除左边还是右边?或者去除其它字符,不发局限于空格.
其实啊,就是把RTRIM()和LTRIM()的活都做了,而且做的好,可以说TRIM()和RTRIM()是TRIM()的子集,代码如下:
- selecttrim('simaopig'),length(trim('simaopig'))aslen;
- /*
- +--------------------+-----+
- |trim('simaopig')|len|
- +--------------------+-----+
- |simaopig|8|
- +--------------------+-----+
- 1rowinset(0.00sec)
- */
- selecttrim(leading'!'from'!!!simaopig!!!')astrim_leading,trim(trailing'!'from'!!!simaopig!!!')astrim_trailing,trim(both'!'from'!!!simaopig!!!')astrim_both;
- /*
- +--------------+---------------+-----------+
- |trim_leading|trim_trailing|trim_both|
- +--------------+---------------+-----------+
- |simaopig!!!|!!!simaopig|simaopig|
- +--------------+---------------+-----------+
- 1rowinset(0.00sec)
- */
LEFT()和RIGHT()函数可以获得字符串的指定部分,它将从字符串的左边或右边返回字符,代码如下:
- selectleft('simaopig',5)asleft_five,right('simaopig',5)asright_five,left(right('simaopig',7),5)asmidd_five;
- /*
- +-----------+------------+-----------+
- |left_five|right_five|midd_five|
- +-----------+------------+-----------+
- |simao|aopig|imaop|
- +-----------+------------+-----------+
- 1rowinset(0.00sec)
- */
SUBSTRING()函数,允许指定一个字符串的长度和起始位置,也是获取子字符串,注意哈,这里的MySQL的字符串起始位置是1,不是零哈,代码如下:
- selectsubstring('simaopig',2,3);
- /*
- +---------------------------+
- |substring('simaopig',2,3)|
- +---------------------------+
- |ima|
- +---------------------------+
- 1rowinset(0.00sec)
- */
CONCAT()函数使其内提供的参数连接起来,如果CONCAT()的参数是空的,其结果也是空的(NULL),代码如下:
- selectconcat('welcome','to','小小子'),concat('simaopig',NULL);
- /*
- +------------------------------------+-------------------------+
- |concat('welcome','to','小小子')|concat('simaopig',NULL)|
- +------------------------------------+-------------------------+
- |welcometo小小子|NULL|
- +------------------------------------+-------------------------+
- 1rowinset(0.00sec)
- */
CONCAT_WS()函数与CONCAT()函数基本相同,只是其还可以提供一个参数作为连接后字符串的分隔符,代码如下:
- selectconcat_ws(',','welcome','to','xiaoxiaozi.com')ascon_wel,concat_ws('','Simaopig','Love','Yatou')ascon_love;
- /*--phpfensi.com
- +---------------------------+---------------------+
- |con_wel|con_love|
- +---------------------------+---------------------+
- |welcome,to,xiaoxiaozi.com|SimaopigLoveYatou|
- +---------------------------+---------------------+
- 1rowinset(0.00sec)
- */
使用LOCATE()函数检验一个字符串是否包含所要检测的字符串,如果存在,返回其第一次出现的位置,今天,第二次强调,MySQL字符串起始位置为1,不是零,代码如下:
- selectlocate('pig','simaopig'),locate('','simaopig'),locate('','simaopig'),locate('','simaopig');
- /*
- +--------------------------+-----------------------+------------------------+--------------------------+
- |locate('pig','simaopig')|locate('','simaopig')|locate('','simaopig')|locate('','simaopig')|
- +--------------------------+-----------------------+------------------------+--------------------------+
- |6|1|0|1|
- +--------------------------+-----------------------+------------------------+--------------------------+
- 1rowinset(0.00sec)
- */
FIND_IN_SET()函数是在一个大集合中查找指定的字符串,在MySQL中看到set就应该想到集合,代码如下:
- selectfind_in_set('simaopig','My,Name,Is,Simaopig,You,Can,Call,Me,simaopig,Too')asstring_locate;
- /*
- +---------------+
- |string_locate|
- +---------------+
- |4|
- +---------------+
- 1rowinset(0.00sec)
- */
哈,看到了吧,其返回的是该字符串是集合的第几个元素,嗯,你还问为啥大写的也返回了?呃,因为我没有用binary啊,STRCMP()函数比较两个字符串是否相同,相同返回0,如果第一个大于第二个返回1,否则返回-1.
呃,同样的,不加binary的话,字符串比较是不区分大小写的,代码如下:
- selectstrcmp('a','b'),strcmp('b','a'),strcmp('a','a'),strcmp('a','A'),strcmp('a','B');
- /*
- +-----------------+-----------------+-----------------+-----------------+-----------------+
- |strcmp('a','b')|strcmp('b','a')|strcmp('a','a')|strcmp('a','A')|strcmp('a','B')|
- +-----------------+-----------------+-----------------+-----------------+-----------------+
- |-1|1|0|0|-1|
- +-----------------+-----------------+-----------------+-----------------+-----------------+
- 1rowinset(0.00sec)
- */
REPLACE()函数和在别的编程语言里的用法一致,替换功能,代码如下:
- selectreplace('Iamsimaopig','simaopig','xiaoxiaozi');
- /*
- +--------------------------------------------------+
- |replace('Iamsimaopig','simaopig','xiaoxiaozi')|
- +--------------------------------------------------+
- |Iamxiaoxiaozi|
- +--------------------------------------------------+
- 1rowinset(0.00sec)
- */
INSSERT()函数可以把一个字符串的指定部分,定义了起始位置和长度,替换为一个新值,代码如下:
- selectinsert('Iamsimaopig',6,8,'xiaoxiaozi');
- /*
- +------------------------------------------+
- |insert('Iamsimaopig',6,8,'xiaoxiaozi')|
- +------------------------------------------+
- |Iamxiaoxiaozi|
- +------------------------------------------+
- 1rowinset(0.00sec)
- */
REPEAT()函数用于重复字符串操作,例如,输出十遍'xiaoxiaozi',代码如下:
- selectrepeat('xiaoxiaozi',10);
- /*
- +------------------------------------------------------------------------------------------------------+
- |repeat('xiaoxiaozi',10)|
- +------------------------------------------------------------------------------------------------------+
- |xiaoxiaozixiaoxiaozixiaoxiaozixiaoxiaozixiaoxiaozixiaoxiaozixiaoxiaozixiaoxiaozixiaoxiaozixiaoxiaozi|
- +------------------------------------------------------------------------------------------------------+
- 1rowinset(0.00sec)
- */
REVERSE()函数颠倒字符串,代码如下:
- selectreverse('simaopig'),reverse(repeat('hello',6));
- /*
- +---------------------+--------------------------------------+
- |reverse('simaopig')|reverse(repeat('hello',6))|
- +---------------------+--------------------------------------+
- |gipoamis|olleholleholleholleholleholleh|
- +---------------------+--------------------------------------+
- 1rowinset(0.00sec)
- */
UCASE()和LCASE()将字符串分别转化为大写和小写,代码如下:
- selectucase('simaopig'),ucase('Simaopig'),lcase('SIMAOPIG'),lcase('simaopiG');
- /*
- +-------------------+-------------------+-------------------+-------------------+
- |ucase('simaopig')|ucase('Simaopig')|lcase('SIMAOPIG')|lcase('simaopiG')|
- +-------------------+-------------------+-------------------+-------------------+
- |SIMAOPIG|SIMAOPIG|simaopig|simaopig|
- +-------------------+-------------------+-------------------+-------------------+
- 1rowinset(0.00sec)
- */
ASCII()函数返回指定字符的ASCII码,代码如下:
- selectascii('y'),ascii('Simaopig'),ascii('simaopig');
- /*
- +------------+-------------------+-------------------+
- |ascii('y')|ascii('Simaopig')|ascii('simaopig')|
- +------------+-------------------+-------------------+
- |121|83|115|
- +------------+-------------------+-------------------+
- 1rowinset(0.00sec)
- */
ORD()函数返回指定字符的数字编码,经常用于替代ASCII(),我咋感觉其返回值与ASCII()一模一样,PHP也有这个函数,貌似,代码如下:
- selectord('y'),ord('Simaopig'),ord('simaopig');
- /*
- +----------+-----------------+-----------------+
- |ord('y')|ord('Simaopig')|ord('simaopig')|
- +----------+-----------------+-----------------+
- |121|83|115|
- +----------+-----------------+-----------------+
- 1rowinset(0.00sec)
- */
再补充一下
1.ASCII(str)
返回字符串str的最左面字符的ASCII代码值,如果str是空字符串,返回0,如果str是NULL,返回NULL.
- mysql>selectASCII('2');
- ->50
- mysql>selectASCII(2);
- ->50
- mysql>selectASCII('dx');
- ->100也可参见ORD()函数。
2.ORD(str)
如果字符串str最左面字符是一个多字节字符,通过以格式((first byte ASCII code)*256+(second byte ASCII code))[*256+third byte ASCII code...]返回字符的ASCII代码值来返回多字节字符代码。如果最左面的字符不是一个多字节字符。返回与ASCII()函数返回的相同值。
mysql> select ORD('2');
-> 50
3.CONV(N,from_base,to_base)
在不同的数字基之间变换数字,返回数字N的字符串数字,从from_base基变换为to_base基,如果任何参数是NULL,返回NULL,参数N解释为一个整数,但是可以指定为一个整数或一个字符串,最小基是2且最大的基是36,如果to_base是一个负数,N被认为是一个有符号数,否则,N被当作无符号数,CONV以64位点精度工作.
- mysql>selectCONV("a",16,2);
- ->'1010'
- mysql>selectCONV("6E",18,8);
- ->'172'
- mysql>selectCONV(-17,10,-18);
- ->'-H'
- mysql>selectCONV(10+"10"+'10'+0xa,10,10);
- ->'40'
4.BIN(N)
返回二进制值N的一个字符串表示,在此N是一个长整数(BIGINT)数字,这等价于CONV(N,10,2),如果N是NULL,返回NULL.
mysql> select BIN(12);
-> '1100'
5.OCT(N)
返回八进制值N的一个字符串的表示,在此N是一个长整型数字,这等价于CONV(N,10,8)。如果N是NULL,返回NULL.
mysql> select OCT(12);
-> '14'
6.HEX(N)
返回十六进制值N一个字符串的表示,在此N是一个长整型(BIGINT)数字,这等价于CONV(N,10,16),如果N是NULL,返回NULL.
mysql> select HEX(255);
-> 'FF'
7.CHAR(N,...)
CHAR()将参数解释为整数并且返回由这些整数的ASCII代码字符组成的一个字符串,NULL值被跳过.
- mysql>selectCHAR(77,121,83,81,'76');
- ->'MySQL'
- mysql>selectCHAR(77,77.3,'77.3');
- ->'MMM'
热门评论