mysql> SELECT LENGTH('hello world') AS length,-> CONCAT('hello','world') AS concat,-> INSERT('hello',1,2,'md'),-> LOWER('HELLO') AS lower,-> TRIM(' hello ') AS trim,-> SUBSTRING('hello',1,2) as sub;+--------+------------+--------------------------+-------+-------+-----+| length | concat | INSERT('hello',1,2,'md')| lower | trim | sub |+--------+------------+--------------------------+-------+-------+-----+| 11 | helloworld | mdllo | hello | hello | he |+--------+------------+--------------------------+-------+-------+-----+
1 row in set
三、日期函数
函数
功能
CURDATE
返回当前系统日期
CURTIME
返回当前系统时间
NOW
返回当前系统日期和时间
MONTH
获取指定日期的月份
WEEK
获取指定日期是一年中的第几周
YEAR
获取年份,返回值范围是 1970〜2069
SUBTIME
时间减法运算,在原始时间上减去指定的时间
ADDTIME
时间加法运算,在原始时间上加上指定的时间
DATEDIFF
获取两个日期的间隔天数
WEEKDAY
返回日期对应的工作日索引
mysql> SELECT CURDATE(),CURTIME(),NOW();+------------+-----------+---------------------+| CURDATE()| CURTIME()| NOW()|+------------+-----------+---------------------+| 2019-10-22 | 10:35:11 | 2019-10-22 10:35:11 |+------------+-----------+---------------------+
1 row in set
mysql> SELECT MONTH('2019-10-22'),YEAR(NOW()),WEEK('2019-10-22',1),WEEKDAY('2019-10-22');+---------------------+-------------+----------------------+-----------------------+| MONTH('2019-10-22')| YEAR(NOW())| WEEK('2019-10-22',1)| WEEKDAY('2019-10-22')|+---------------------+-------------+----------------------+-----------------------+| 10 | 2019 | 43 | 1 |+---------------------+-------------+----------------------+-----------------------+
1 row in set
mysql> SELECT SUBTIME('2019-10-22 23:59:59','0:1:1') AS sub,-> ADDTIME('2019-10-22 23:59:59','0:1:1') AS addT,-> DATEDIFF('2019-10-20','2019-10-22') AS diff;+---------------------+---------------------+------+| sub | addT |diff|+---------------------+---------------------+------+| 2019-10-22 23:58:58 | 2019-10-23 00:01:00 |-2 |+---------------------+---------------------+------+
1 row in set
mysql> SELECT IFNULL(5,8),IFNULL(NULL,'OK'),IFNULL(SQRT(-8),'FALSE'),SQRT(-8);+-------------+-------------------+--------------------------+----------+| IFNULL(5,8)| IFNULL(NULL,'OK')| IFNULL(SQRT(-8),'FALSE')| SQRT(-8)|+-------------+-------------------+--------------------------+----------+| 5 | OK | FALSE | NULL |+-------------+-------------------+--------------------------+----------+
1 row in set
SELECT CASE WEEKDAY(NOW()) WHEN 0 THEN '星期一' WHEN 1 THEN '星期二' WHEN
2 THEN '星期三' WHEN 3 THEN '星期四' WHEN 4 THEN '星期五' WHEN 5 THEN '星期六'ELSE'星期天'END AS COLUMN1,NOW(),WEEKDAY(NOW()),DAYNAME(NOW());+---------+---------------------+----------------+----------------+| COLUMN1 | NOW()| WEEKDAY(NOW())| DAYNAME(NOW())|+---------+---------------------+----------------+----------------+| 星期二 | 2019-10-22 10:54:57 | 1 | Tuesday |+---------+---------------------+----------------+----------------+
1 row in set