文本处理函数与日期时间处理函数

一、文本处理函数

  1. LENGTH():以字节为单位返回字符串的长度。
mysql> SELECT LENGTH('1234');
+----------------+
| LENGTH('1234') |
+----------------+
|              4 |
+----------------+
1 row in set (0.00 sec)
  1. 大小写转换,UPPER()转换为大写,LOWER()转换为小写。
mysql> SELECT LOWER('Hello'), UPPER('Hello');
+----------------+----------------+
| LOWER('Hello') | UPPER('Hello') |
+----------------+----------------+
| hello          | HELLO          |
+----------------+----------------+
1 row in set (0.00 sec)
  1. 获取子字符串。

LEFT()返回从左边取指定长度的子串,RIGHT()返回从右边取指定长度的子串。

mysql> SELECT LEFT('1234567890', 3) AS left_3, RIGHT('1234567890', 3) AS right_3;
+--------+---------+
| left_3 | right_3 |
+--------+---------+
| 123    | 890     |
+--------+---------+
1 row in set (0.00 sec)

SUBSTRING()返回指定的子字符串,参数可以为起始位置或起始位置和长度,起始位置为负数。

mysql> SELECT SUBSTRING('function',2),SUBSTRING('function',2,4),SUBSTRING('function',-4,2);
+-------------------------+---------------------------+----------------------------+
| SUBSTRING('function',2) | SUBSTRING('function',2,4) | SUBSTRING('function',-4,2) |
+-------------------------+---------------------------+----------------------------+
| unction                 | unct                      | ti                         |
+-------------------------+---------------------------+----------------------------+
1 row in set (0.00 sec)
  1. 去除空格。

LTRIM()去除左边空格,RTRIM()去除右边空格,TRIM()去除两边空格。

mysql> SELECT CONCAT('(',LTRIM('  hello  '),')') AS ltrim , 
    CONCAT('(',RTRIM('  hello  '),')') AS rtrim , 
    CONCAT('(',TRIM('  hello  '),')') AS trim ;
+-----------+-----------+---------+
| ltrim     | rtrim     | trim    |
+-----------+-----------+---------+
| (hello  ) | (  hello) | (hello) |
+-----------+-----------+---------+
1 row in set (0.00 sec)
  1. 查找子字符串第一次出现的位置。

LOCATE(substr,str), LOCATE(substr,str,pos),INSTR(str,substr),两种函数的参数出现的顺序不同。

mysql> SELECT LOCATE('bar', 'foobarbar'),LOCATE('xbar', 'foobar'),LOCATE('bar', 'foobarbar', 5),INSTR('foobarbar', 'bar');
+----------------------------+--------------------------+-------------------------------+---------------------------+
| LOCATE('bar', 'foobarbar') | LOCATE('xbar', 'foobar') | LOCATE('bar', 'foobarbar', 5) | INSTR('foobarbar', 'bar') |
+----------------------------+--------------------------+-------------------------------+---------------------------+
|                          4 |                        0 |                             7 |                         4 |
+----------------------------+--------------------------+-------------------------------+---------------------------+
1 row in set (0.00 sec)

二、日期和时间处理函数

  1. Now()返回当前日期和时间。
mysql>  select Now();
+---------------------+
| Now()               |
+---------------------+
| 2019-03-31 23:49:02 |
+---------------------+
1 row in set (0.01 sec)
  1. CurDate()返回当前日期。
mysql> select CurDate();
+------------+
| CurDate()  |
+------------+
| 2019-03-31 |
+------------+
1 row in set (0.00 sec)
  1. CurTime()返回当前时间。
mysql> select CurTime();
+-----------+
| CurTime() |
+-----------+
| 23:52:03  |
+-----------+
1 row in set (0.00 sec)
  1. DATE()提取日期或日期/时间表达式的日期部分。DAY()返回一个日期的天数部分。YEAR()返回一个日期的年份部分。
mysql> SELECT DATE('2003-12-31 01:02:03'), DAY('2007-02-03'), YEAR('2007-02-03');
+-----------------------------+-------------------+--------------------+
| DATE('2003-12-31 01:02:03') | DAY('2007-02-03') | YEAR('2007-02-03') |
+-----------------------------+-------------------+--------------------+
| 2003-12-31                  |                 3 |               2007 |
+-----------------------------+-------------------+--------------------+
1 row in set (0.00 sec)
  1. DATE_ADD()给日期添加指定的时间间隔,DATE_SUB()从日期减去指定的时间间隔。两者的用法相同。
mysql> SELECT DATE_ADD('2018-05-01',INTERVAL 30 DAY), DATE_ADD('2025-01-01 00:00:00',INTERVAL '1 1:1:1' DAY_SECOND);
+----------------------------------------+---------------------------------------------------------------+
| DATE_ADD('2018-05-01',INTERVAL 30 DAY) | DATE_ADD('2025-01-01 00:00:00',INTERVAL '1 1:1:1' DAY_SECOND) |
+----------------------------------------+---------------------------------------------------------------+
| 2018-05-31                             | 2025-01-02 01:01:01                                           |
+----------------------------------------+---------------------------------------------------------------+
1 row in set (0.00 sec)

另外,ADDDATE()为增加一个日期(天,周等),ADDTIME()增加一个时间(时,分等),功能与DATE_ADD()类似,当为以下第一种形式时则完全相同。同样地,SUBDATE()与DATE_SUB()类似。

mysql> SELECT ADDDATE('2008-01-02', INTERVAL 31 DAY), ADDDATE('2008-01-02', 31), ADDTIME('01:00:00', '02:00:05');
+----------------------------------------+---------------------------+---------------------------------+
| ADDDATE('2008-01-02', INTERVAL 31 DAY) | ADDDATE('2008-01-02', 31) | ADDTIME('01:00:00', '02:00:05') |
+----------------------------------------+---------------------------+---------------------------------+
| 2008-02-02                             | 2008-02-02                | 03:00:05                        |
+----------------------------------------+---------------------------+---------------------------------+
1 row in set (0.00 sec)
  1. DATEDIFF()返回两个日期之间的天数。
mysql> SELECT DATEDIFF('2007-12-31 23:59:59','2007-12-30'), DATEDIFF('2007-11-30 23:59:59','2007-12-31');
+----------------------------------------------+----------------------------------------------+
| DATEDIFF('2007-12-31 23:59:59','2007-12-30') | DATEDIFF('2007-11-30 23:59:59','2007-12-31') |
+----------------------------------------------+----------------------------------------------+
|                                            1 |                                          -31 |
+----------------------------------------------+----------------------------------------------+
1 row in set (0.00 sec)
  1. DATE_FORMAT()用不同的格式显示日期/时间。其中%W等为说明符,可以通过查表了解不同说明符的作用。
mysql> SELECT DATE_FORMAT('2009-10-04 22:23:00', '%W %M %Y %H:%i:%s');
+---------------------------------------------------------+
| DATE_FORMAT('2009-10-04 22:23:00', '%W %M %Y %H:%i:%s') |
+---------------------------------------------------------+
| Sunday October 2009 22:23:00                            |
+---------------------------------------------------------+
1 row in set (0.00 sec)

你可能感兴趣的:(文本处理函数与日期时间处理函数)