MySQL存储过程的基本函数(三)

(1).字符串类

首先定义一个字符串变量:set @str="lxl";

CHARSET(str) //返回字串字符集

 select charset(@str);
+---------------+
| charset(@str) |
+---------------+
| utf8          |
+---------------+


CONCAT (string [,... ]) //连接字串

select concat(@str,"hello");
+----------------------+
| concat(@str,"hello") |
+----------------------+
| abcdefghello            |
+----------------------+


INSTR (string ,substring ) //返回substring首次在string中出现的位置,不存在返回0

select instr(@str,‘de‘);
+------------------+
| instr(@str,‘de‘) |
+------------------+
|                4 |
+------------------+

注:string的下标从1开始,并不是0。

select instr(@str,‘oo‘);
+------------------+
| instr(@str,‘oo‘) |
+------------------+
|                0 |
+------------------+

 

LOCATE (substring , string [,start_position ] ) 同INSTR,但可指定开始位置

select locate(‘de‘,@str,2);
+---------------------+
| locate(‘de‘,@str,2) |
+---------------------+
|                   4 |
+---------------------+


LCASE (string )或者 lower(string),//转换成小写

set @str1="ASDFD";

 select lcase(@str1);
+--------------+
| lcase(@str1) |
+--------------+
| asdfd        |
+--------------+

select lower(@str1);
+--------------+
| lower(@str1) |
+--------------+
| asdfd        |
+--------------+

 

UASE (string )或者 uppper(string),//转换成大写

select ucase(@str1);
+--------------+
| ucase(@str1) |
+--------------+
| ASDFD        |
+--------------+

select upper(@str1);
+--------------+
| upper(@str1) |
+--------------+
| ASDFD        |
+--------------+


LEFT (string ,length ) //从string2中的左边起取length个字符

set @str1="asdfd";

select left(@str,3);
+--------------+
| left(@str,3) |
+--------------+
| abc          |
+--------------+

 

RIGHT(string,length) //取string最后length个字符

select right(‘adfsfsdf‘,3);
+---------------------+
| right(‘adfsfsdf‘,3) |
+---------------------+
| sdf                 |
+---------------------+


LENGTH (string ) //string长度

select length(@str);
+--------------+
| length(@str) |
+--------------+
|            7 |
+--------------+


LOAD_FILE (file_name ) //从文件读取内容

 在桌面新建一个test.txt文件,“aaaaaaaaaaaaaaaa你好“

select load_file(@path);
+----------------------+
| load_file(@path)     |
+----------------------+
| aaaaaaaaaaaaaaaa你好|
+----------------------+

LPAD (string ,length ,pad ) //重复用pad加在string开头,直到字串长度为length

set @str1=‘ASDFD‘;

select lpad(@str1,20,‘xx‘);
+----------------------+
| lpad(@str1,20,‘xx‘)  |
+----------------------+
| xxxxxxxxxxxxxxxASDFD |
+----------------------+


RPAD (string ,length ,pad) //在str后用pad补充,直到长度为length

select rpad(@str,20,‘xx‘);
+----------------------+
| rpad(@str,20,‘xx‘)   |
+----------------------+
| abcdefgxxxxxxxxxxxxx |
+----------------------+


LTRIM (string) //去除前端空格

set @str1="   ASDFD";

 select ltrim(@str1);
+--------------+
| ltrim(@str1) |
+--------------+
| ASDFD        |
+--------------+

 

RTRIM (string ) //去除后端空格

set @str1="asssss   ";

select rtrim(@str1);
+--------------+
| rtrim(@str1) |
+--------------+
| asssss       |
+--------------+

 

TRIM([[BOTH|LEADING|TRAILING] [padding] FROM]string2) //去除指定位置的指定字符

默认删除前后空格:

 select trim(‘  brrr  ‘);
+------------------+
| trim(‘  brrr  ‘) |
+------------------+
| brrr             |
+------------------+

删除指定首字符 如’,‘

select trim(leading ‘,‘ from ‘,,,,dfdfdf,,,,‘);
+-----------------------------------------+
| trim(leading ‘,‘ from ‘,,,,dfdfdf,,,,‘) |
+-----------------------------------------+
| dfdfdf,,,,                              |
+-----------------------------------------+

删除指定首尾字符 如’,‘

select trim(trailing ‘,‘ from ‘,,,,sdfdfd,,,,‘);
+------------------------------------------+
| trim(trailing ‘,‘ from ‘,,,,sdfdfd,,,,‘) |
+------------------------------------------+
| ,,,,sdfdfd                               |
+------------------------------------------+

 删除指定首尾字符

select trim(both ‘,‘ from ‘,,,,sdfsdfsdf,,,,‘);
+-----------------------------------------+
| trim(both ‘,‘ from ‘,,,,sdfsdfsdf,,,,‘) |
+-----------------------------------------+
| sdfsdfsdf                               |
+-----------------------------------------+


REPEAT (string ,count ) //将string重复count次

select repeat(@str,3);
+-----------------------+
| repeat(@str,3)        |
+-----------------------+
| abcdefgabcdefgabcdefg |
+-----------------------+


REPLACE (str ,search_str ,replace_str ) //在str中用replace_str替换search_str

 select replace(@str,‘abc‘,‘aaa‘);
+---------------------------+
| replace(@str,‘abc‘,‘aaa‘) |
+---------------------------+
| aaadefg                   |
+---------------------------+


STRCMP (string1 ,string2 ) //逐字符比较两字串大小

 set @str="abcdefg";

set @str1="aaaaaaaaaaa";

 select strcmp(@str,@str1);
+--------------------+
| strcmp(@str,@str1) |
+--------------------+
|                  1 |
+--------------------+

 

set @str1="bbbb";

 select strcmp(@str,@str1);
+--------------------+
| strcmp(@str,@str1) |
+--------------------+
|                 -1 |
+--------------------+

若第一个string大返回1,否则返回-1。


SUBSTRING (str , position [,length ]) //从str的position开始,取length个字符

 set @str="abcdefg";

select substring(@str,2,3);
+---------------------+
| substring(@str,2,3) |
+---------------------+
| bcd                 |
+---------------------+


SPACE(count) //生成count个空格

select concat(space(5),‘aaaa‘);
+-------------------------+
| concat(space(5),‘aaaa‘) |
+-------------------------+
|      aaaa               |
+-------------------------+

 

(2).数学类

ABS (number2 ) //绝对值

 select abs(-3);
+---------+
| abs(-3) |
+---------+
|       3 |
+---------+
BIN (decimal_number ) //十进制转二进制

select bin(8);
+--------+
| bin(8) |
+--------+
| 1000   |
+--------+


CEILING (number2 ) //向上取整

select ceiling(3.2);
+--------------+
| ceiling(3.2) |
+--------------+
|            4 |
+--------------+

 

FLOOR (number2 ) //向下取整

 select floor(4.999);
+--------------+
| floor(4.999) |
+--------------+
|            4 |
+--------------+


CONV(number2,from_base,to_base) //进制转换

select conv(8,10,2);
+--------------+
| conv(8,10,2) |
+--------------+
| 1000         |
+--------------+

 select conv(17,10,16);
+----------------+
| conv(17,10,16) |
+----------------+
| 11             |
+----------------+


FORMAT (number,decimal_places ) //保留小数位数,会四舍五入

select format(4.12367823,3);
+----------------------+
| format(4.12367823,3) |
+----------------------+
| 4.124                |
+----------------------+


HEX (DecimalNumber ) //转十六进制

select hex(18);
+---------+
| hex(18) |
+---------+
| 12      |
+---------+

 select hex(‘你好吗‘);
+--------------------+
| hex(‘你好吗‘)      |
+--------------------+
| E4BDA0E5A5BDE59097 |
+--------------------+
:HEX()中可传入字符串,则返回其ASC-11码,如HEX(‘DEF‘)返回4142143
也可以传入十进制整数,返回其十六进制编码,如HEX(25)返回19


LEAST (number , number2 [,..]) //求最小值

 select least(2,4,6,3,8,5);
+--------------------+
| least(2,4,6,3,8,5) |
+--------------------+
|                  2 |
+--------------------+


MOD (numerator ,denominator ) //求余

select mod(10,3);
+-----------+
| mod(10,3) |
+-----------+
|         1 |
+-----------+


POWER (number ,power ) //求指数

select power(2,3);
+------------+
| power(2,3) |
+------------+
|          8 |
+------------+
RAND([seed]) //随机数
ROUND (number [,decimals ]) //四舍五入,decimals为小数位数]

注:返回类型并非均为整数,如:
(1)默认变为整形值

    mysql> select round(1.23); 
    +-------------+ 
    | round(1.23) | 
    +-------------+ 
    |           1 | 
    +-------------+ 
    1 row in set (0.00 sec) 
    
    mysql> select round(1.56); 
    +-------------+ 
    | round(1.56) | 
    +-------------+ 
    |           2 | 
    +-------------+ 
    1 row in set (0.00 sec)



(2)可以设定小数位数,返回浮点型数据

    mysql> select round(1.567,2); 
    +----------------+ 
    | round(1.567,2) | 
    +----------------+ 
    |           1.57 | 
    +----------------+ 
    1 row in set (0.00 sec)

SIGN (number2 ) //

 

MySQL存储过程的基本函数(三),布布扣,bubuko.com

MySQL存储过程的基本函数(三)

上一篇:delphi中我用定时器每隔一段时间执行操作


下一篇:angular笔记_4(函数)