(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) //开平方

MySQL存储过程的基本函数(三)的更多相关文章

  1. MySQL存储过程的基本函数

    (1).字符串类 CHARSET(str) //返回字串字符集 CONCAT (string2 [,... ]) //连接字串 INSTR (string ,substring ) //返回subst ...

  2. Mysql存储过程语法

    一口气弄完了! 一.条件语句if-then-else: create procedure demo_1(in param int) begin declare var int; ; then inse ...

  3. MySQL存储过程学习笔记

    MySQL在5.0以前并不支持存储过程,这使得MySQL在应用上大打折扣.MySQL 5.0终于开始支持存储过程了. MySQL的关键字大小写通用.该学习笔记对关键字使用大写:变量名,表名使用小写. ...

  4. mysql 存储过程详解

    MySQL 存储过程是从 MySQL 5.0 开始增加的新功能.存储过程的优点有一箩筐.不过最主要的还是执行效率和SQL 代码封装.特别是 SQL 代码封装功能,如果没有存储过程,在外部程序访问数据库 ...

  5. mysql存储过程详解

    mysql存储过程详解 1.      存储过程简介   我们常用的操作数据库语言SQL语句在执行的时候需要要先编译,然后执行,而存储过程(Stored Procedure)是一组为了完成特定功能的S ...

  6. mysql存储过程详细教程

    记录mysql存储过程中的关键语法:DELIMITER //  声明语句结束符,用于区分;CREATE PROCEDURE demo_in_parameter(IN p_in int)  声明存储过程 ...

  7. MySql存储过程学习总结

    创建存储过程 1.格式   MySQL存储过程创建的格式:CREATE PROCEDURE 过程名 ([过程参数[,...]]),举个例子: CREATE PROCEDURE proc1 (OUT s ...

  8. mysql 存储过程详解 存储过程

    mysql存储过程详解 1.      存储过程简介         我们常用的操作数据库语言SQL语句在执行的时候需要要先编译,然后执行,而存储过程(Stored Procedure)是一组为了完成 ...

  9. MySQL存储过程详解 mysql 存储过程

    原文地址:MySQL存储过程详解  mysql 存储过程作者:王者佳暮 mysql存储过程详解 1.     存储过程简介 我们常用的操作数据库语言SQL语句在执行的时候需要要先编译,然后执行,而存储 ...

随机推荐

  1. spoj 2319 BIGSEQ - Sequence

    You are given the sequence of all K-digit binary numbers: 0, 1,..., 2K-1. You need to fully partitio ...

  2. 【Java】Web 服务编程技巧与窍门: 在 UDDI 注册中心为 Web 服务注册开发 UDDI Java 应用程序

    本技巧建立了一个使用统一描述.发现和集成 (Universal Description, Discovery, and Integration,UDDI) 来注册应用程序级消费的 Web 服务实例.作 ...

  3. linux 修改目录文件权限,目录文件所属用户,用户组

    1:查看命令:ll drwxr-xr-x  4 gamer ftp      4096 Mar  7 16:56 gstore drwxrwxrwx 10 root  ftp      4096 De ...

  4. ubuntu 查本机 ip地址的命令是什么, 详细信息的?

    使用ifconfig命令即可.你一敲进去都出来了

  5. sphinx插入css

    使用role指令达到目的. We can put following lines at the beginning of our RST file to specify its style. .. r ...

  6. Effective c++ 第一章 让自己习惯C++

    条款 01:c++是一个语言联邦而不是一种单一的语言, 它包括: 1.C语言:没有模版.没有异常.没有重载…… 2.Object-Oriented C++:class.析构函数.构造函数.封装.继承. ...

  7. bzoj2783

    由于路径的深度是升序的所以我们可以考虑用前缀和的思想,用sum维护点到根路径上节点和对于每个点x存在路径和为s即这个点到根的路径上存在y,使sum[x]-sum[y]=s这显然是可以二分的 type ...

  8. python对拍程序

    import sys; import random; import os; gen=open("data.in","w"); #///生成测试数据 gen.cl ...

  9. C语言基础课程 第二课 HelloWorld不为菜鸟所知的秘密

    1    愉快的开端hello world    4 1.1    include头文件包含    4 1.2    main函数    4 1.3    注释    4 1.4    {}括号,程序 ...

  10. wpa_cli 连接 wifi

    转自:http://hi.baidu.com/yyangjjun/item/9dfe8e175439fc7a1009b5ba   1: run wpa_supplicant first use the ...