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]) //随机数

select rand()*3;
+------------------+
| rand()*3         |
+------------------+
| 2.98752005862772 |
+------------------+

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 ) //符号函数,根据数值返回1(正数) 0(零) -1(负数).

select sign(1);
+---------+
| sign(1) |
+---------+
|       1 |
+---------+

select sign(0);
+---------+
| sign(0) |
+---------+
|       0 |
+---------+

select sign(-1990);
+-------------+
| sign(-1990) |
+-------------+
|          -1 |
+-------------+

(3).日期时间类
ADDTIME (date2 ,time_interval ) //将time_interval加到date2
CONVERT_TZ (datetime2 ,fromTZ ,toTZ ) //转换时区
CURRENT_DATE ( ) //当前日期
CURRENT_TIME ( ) //当前时间
CURRENT_TIMESTAMP ( ) //当前时间戳
DATE (datetime ) //返回datetime的日期部分
DATE_ADD (date2 , INTERVAL d_value d_type ) //在date2中加上日期或时间
DATE_FORMAT (datetime ,FormatCodes ) //使用formatcodes格式显示datetime
DATE_SUB (date2 , INTERVAL d_value d_type ) //在date2上减去一个时间
DATEDIFF (date1 ,date2 ) //两个日期差
DAY (date ) //返回日期的天
DAYNAME (date ) //英文星期
DAYOFWEEK (date ) //星期(1-7) ,1为星期天
DAYOFYEAR (date ) //一年中的第几天
EXTRACT (interval_name FROM date ) //从date中提取日期的指定部分
MAKEDATE (year ,day ) //给出年及年中的第几天,生成日期串
MAKETIME (hour ,minute ,second ) //生成时间串
MONTHNAME (date ) //英文月份名
NOW ( ) //当前时间
SEC_TO_TIME (seconds ) //秒数转成时间
STR_TO_DATE (string ,format ) //字串转成时间,以format格式显示
TIMEDIFF (datetime1 ,datetime2 ) //两个时间差
TIME_TO_SEC (time ) //时间转秒数]
WEEK (date_time [,start_of_week ]) //第几周
YEAR (datetime ) //年份
DAYOFMONTH(datetime) //月的第几天
HOUR(datetime) //小时
LAST_DAY(date) //date的月的最后日期
MICROSECOND(datetime) //微秒
MONTH(datetime) //月
MINUTE(datetime) //分返回符号,正负或0
SQRT(number2) //开平方

上一篇:TR069网管测试华为ITMS平台(内部测试使用)


下一篇:mysql存储过程详细教程