MySQL常用命令(六)-文本处理函数、日期处理函数、数值处理函数

LEFT(str,length)

作用:取出串str左边长度为length的子串;

RIGHT(str,length)

作用:取出串str右边边长度为length的子串;

SUBSTRING(str,startIndex)

作用:取出str,从第startIndex开始到末尾位置长度的串;

SUBSTRING(str,startIndex,length)

作用:取出str,从第startIndex开始长度为length的串;

UPPER(str)

作用:将字符串str转为大写;

LOWER()

作用:将字符串str转为小写形式;

LENGTH()

作用:显示字符串str的长度;

LOCATE(substr,str)

作用:找出substr子串在str中第一次出现的位置;

mysql> SELECT LEFT('www.baidu.com',4);
+-------------------------+
| LEFT('www.baidu.com',4) |
+-------------------------+
| www.                    |
+-------------------------+
1 row in set (0.00 sec)

mysql> SELECT RIGHT('www.baidu.com',4);
+--------------------------+
| RIGHT('www.baidu.com',4) |
+--------------------------+
| .com                     |
+--------------------------+
1 row in set (0.00 sec)

mysql> SELECT SUBSTRING('www.baidu.com',4);
+------------------------------+
| SUBSTRING('www.baidu.com',4) |
+------------------------------+
| .baidu.com                   |
+------------------------------+
1 row in set (0.00 sec)

mysql> SELECT SUBSTRING('www.baidu.com',4,3);
+--------------------------------+
| SUBSTRING('www.baidu.com',4,3) |
+--------------------------------+
| .ba                            |
+--------------------------------+
1 row in set (0.00 sec)
+------------------------+
| UPPER('www.baidu.com') |
+------------------------+
| WWW.BAIDU.COM          |
+------------------------+
1 row in set (0.00 sec)

mysql> SELECT LOWER('WWW.BAIDU.COM');
+------------------------+
| LOWER('WWW.BAIDU.COM') |
+------------------------+
| www.baidu.com          |
+------------------------+
1 row in set (0.00 sec)

mysql> SELECT LENGTH('WWW.BAIDU.COM');
+-------------------------+
| LENGTH('WWW.BAIDU.COM') |
+-------------------------+
|                      13 |
+-------------------------+
1 row in set (0.00 sec)

mysql> SELECT LOCATE('bai','www.baidu.com');
+-------------------------------+
| LOCATE('bai','www.baidu.com') |
+-------------------------------+
|                             5 |
+-------------------------------+
1 row in set (0.00 sec)

关于日期

函数 说明
ADDDATE() 增加一个日期(天、周),也可以使用INTERVAL类似于DATE_ADD()
ADDTIME() 增加一个时间(时、分等)
CURDATE() 返回当前日期
CURTIME() 返回当前日期
DATE() 返回日期时间的日期部分
DATEDIFF() 计算两个日期之间的差
DATE_ADD() 非常灵活的日期函数
DATE_FORMAT() 日期格式化
DAY() 返回日期的天数部分
DAYOFFWEEK() 返回星期几,1表示周日;
HOUR() 返回日期时间的小时部分
MINUTE() 返回日期时间的分部分;
MONTH() 返回日期时间的月份
NOW() 返回当前日期时间
SECOND() 返回日期时间的秒
TIME() 返回日期时间的时间部分
YEAR() 返回一个日期的年份部分;
mysql> SELECT CURDATE();# 返回当前的日期,不包括时间;
+------------+
| CURDATE()  |
+------------+
| 2020-05-04 |
+------------+
1 row in set (0.00 sec)

mysql> SELECT CURTIME();#返回当前的时间;
+-----------+
| CURTIME() |
+-----------+
| 18:52:01  |
+-----------+
1 row in set (0.00 sec)

mysql> SELECT DATE(NOW());#返回日期部分;
+-------------+
| DATE(NOW()) |
+-------------+
| 2020-05-04  |
+-------------+
1 row in set (0.00 sec)

mysql> SELECT DAY(NOW());#返回日期的天数部分;
+------------+
| DAY(NOW()) |
+------------+
|          4 |
+------------+
1 row in set (0.00 sec)

mysql> SELECT HOUR(NOW());#返回时间的小时部分;
+-------------+
| HOUR(NOW()) |
+-------------+
|          18 |
+-------------+
1 row in set (0.00 sec)

mysql> SELECT MINUTE(NOW());#返回时间的分部分;
+---------------+
| MINUTE(NOW()) |
+---------------+
|            56 |
+---------------+
1 row in set (0.00 sec)

mysql> SELECT SECOND(NOW());#返回时间的秒部分;
+---------------+
| SECOND(NOW()) |
+---------------+
|            22 |
+---------------+
1 row in set (0.00 sec)

mysql> SELECT MONTH(NOW());#返回日期的月部分;
+--------------+
| MONTH(NOW()) |
+--------------+
|            5 |
+--------------+
1 row in set (0.00 sec)

mysql> SELECT TIME(NOW());#返回时间部分;
+-------------+
| TIME(NOW()) |
+-------------+
| 18:56:48    |
+-------------+
1 row in set (0.00 sec)

mysql> SELECT YEAR(NOW());#返回日期的年份;
+-------------+
| YEAR(NOW()) |
+-------------+
|        2020 |
+-------------+
1 row in set (0.00 sec)

mysql> SELECT DAYOFWEEK(NOW());#返回星期几,这里周日是1;
+------------------+
| DAYOFWEEK(NOW()) |
+------------------+
|                2 |
+------------------+
1 row in set (0.00 sec)
mysql> SELECT DATE_FORMAT(NOW(),'%Y/%m/%d');#格式化日期时间;
+-------------------------------+
| DATE_FORMAT(NOW(),'%Y/%m/%d') |
+-------------------------------+
| 2020/05/04                    |
+-------------------------------+
1 row in set (0.00 sec)
mysql> SELECT DATE_ADD(NOW(),INTERVAL 2 DAY);#更改日期时间,比较灵活,INTERVAL。
+--------------------------------+
| DATE_ADD(NOW(),INTERVAL 2 DAY) |
+--------------------------------+
| 2020-05-06 19:06:03            |
+--------------------------------+
1 row in set (0.00 sec)

mysql> SELECT ADDDATE(NOW(),2);#增加一个日期;
+---------------------+
| ADDDATE(NOW(),2)    |
+---------------------+
| 2020-05-06 19:08:09 |
+---------------------+
1 row in set (0.00 sec)

mysql> SELECT ADDDATE(NOW(),INTERVAL 2 DAY);#可以和DATE_ADD一样使用;
+-------------------------------+
| ADDDATE(NOW(),INTERVAL 2 DAY) |
+-------------------------------+
| 2020-05-06 19:08:20           |
+-------------------------------+
1 row in set (0.00 sec)

mysql> SELECT DATEDIFF(NOW(),'2020-09-01');#日期相减;
+------------------------------+
| DATEDIFF(NOW(),'2020-09-01') |
+------------------------------+
|                         -120 |
+------------------------------+
1 row in set (0.00 sec)

mysql> SELECT ADDTIME(NOW(),'1:00:00');#新增时间,时分秒等;
+--------------------------+
| ADDTIME(NOW(),'1:00:00') |
+--------------------------+
| 2020-05-04 20:13:59      |
+--------------------------+
1 row in set (0.00 sec)

数值处理函数

函数 说明
ABS() 返回绝对值
COS() 余弦值;
SIN() 正弦值;
TAN() 正切值
EXP() 指数值
MOD() 两个值的余数(*)
PI() 圆周率(*)
RAND() 返回一个随机数;(*)
SQRT() 平方根
mysql> SELECT ABS(-1);
+---------+
| ABS(-1) |
+---------+
|       1 |
+---------+
1 row in set (0.00 sec)

mysql> SELECT COS(90);
+---------------------+
| COS(90)             |
+---------------------+
| -0.4480736161291701 |
+---------------------+
1 row in set (0.00 sec)
mysql> SELECT EXP(10);
+--------------------+
| EXP(10)            |
+--------------------+
| 22026.465794806718 |
+--------------------+
1 row in set (0.00 sec)
mysql> SELECT MOD(11,2);
+-----------+
| MOD(11,2) |
+-----------+
|         1 |
+-----------+
1 row in set (0.00 sec)

mysql> SELECT PI();
+----------+
| PI()     |
+----------+
| 3.141593 |
+----------+
1 row in set (0.00 sec)

mysql> SELECT RAND();
+--------------------+
| RAND()             |
+--------------------+
| 0.7575842177100333 |
+--------------------+
1 row in set (0.00 sec)

mysql> SELECT SIN(90)
    -> ;
+--------------------+
| SIN(90)            |
+--------------------+
| 0.8939966636005579 |
+--------------------+
1 row in set (0.00 sec)

mysql> SELECT SQRT(4);
+---------+
| SQRT(4) |
+---------+
|       2 |
+---------+
1 row in set (0.00 sec)

mysql> SELECT TAN(45)
    -> ;
+--------------------+
| TAN(45)            |
+--------------------+
| 1.6197751905438615 |
+--------------------+
1 row in set (0.00 sec)

你可能感兴趣的:(MySQL常用命令(六)-文本处理函数、日期处理函数、数值处理函数)