MySQL常用日期处理函数和文本处理函数

常见日期处理函数

  • NOW([fsp])返回当前的日期和时间,格式为:'YYYY-MM-DD HH:MM:SS' 或 'YYYYMMDDHHMMSS'。具体使用哪种格式取决于NOW()函数使用的环境(字符串/数字)。

    mysql> SELECT NOW();
    +---------------------+
    | NOW()               |
    +---------------------+
    | 2019-03-28 06:46:39 |
    +---------------------+
    1 row in set (0.00 sec)
    
    mysql> SELECT NOW() + 0;
    +----------------+
    | NOW() + 0      |
    +----------------+
    | 20190328064659 |
    +----------------+
    1 row in set (0.00 sec)
    

    还可以在函数中提供一个fsp参数(0-6)来指定秒数的精度:

    mysql> SELECT NOW(6);
    +----------------------------+
    | NOW(6)                     |
    +----------------------------+
    | 2019-03-28 06:47:11.620058 |
    +----------------------------+
    1 row in set (0.00 sec)
    

    NOW()SYSDATE()都可以返回当前时间,二者的区别在于,NOW()返回的时包含该函数的语句开始运行的时间,而SYSDATE()返回的是函数执行的时间。另一个区别是,在执行SET TIMESTAMP语句后,SYSDATE()的返回值不受影响,而NOW()则会返回设定的值。

  • CURDATE()返回当前的日期,格式为:'YYYY-MM-DD' 或 'YYYYMMDD'。具体使用哪种格式取决于该函数使用的环境(字符串/数字)。

    mysql> SELECT CURDATE();
    +------------+
    | CURDATE()  |
    +------------+
    | 2019-03-28 |
    +------------+
    1 row in set (0.01 sec)
    
    mysql> SELECT CURDATE() + 0;
    +---------------+
    | CURDATE() + 0 |
    +---------------+
    |      20190328 |
    +---------------+
    1 row in set (0.00 sec)
    
  • CURTIME([fsp])返回当前的时间,格式为:'HH:MM:SS' 或 'HHMMSS'。具体使用哪种格式取决于该函数使用的环境(字符串/数字)。还可以在函数中提供一个fsp参数(0-6)来指定秒数的精度:

    mysql> SELECT CURTIME();
    +-----------+
    | CURTIME() |
    +-----------+
    | 07:03:54  |
    +-----------+
    1 row in set (0.00 sec)
    
    mysql> SELECT CURTIME() + 0;
    +---------------+
    | CURTIME() + 0 |
    +---------------+
    |         70404 |
    +---------------+
    1 row in set (0.00 sec)
    
    mysql> SELECT CURTIME(6) + 0;
    +----------------+
    | CURTIME(6) + 0 |
    +----------------+
    |   70416.578713 |
    +----------------+
    1 row in set (0.00 sec)
    
  • DATE(expr)提取日期表达式/日期时间表达式中的日期部分。

    mysql> SELECT DATE('2010-10-05 09:29:45');
    +-----------------------------+
    | DATE('2010-10-05 09:29:45') |
    +-----------------------------+
    | 2010-10-05                  |
    +-----------------------------+
    1 row in set (0.00 sec)
    
  • EXTRACT(unit FROM date)从日期表达式/日期时间表达式中提取给定的unit。

    mysql> SELECT EXTRACT(YEAR FROM '2008-09-12');
    +---------------------------------+
    | EXTRACT(YEAR FROM '2008-09-12') |
    +---------------------------------+
    |                            2008 |
    +---------------------------------+
    1 row in set (0.02 sec)
    
    mysql> SELECT EXTRACT(QUARTER FROM '2008-09-12');
    +------------------------------------+
    | EXTRACT(QUARTER FROM '2008-09-12') |
    +------------------------------------+
    |                                  3 |
    +------------------------------------+
    1 row in set (0.00 sec)
    
    mysql> SELECT EXTRACT(YEAR_MONTH FROM '2008-09-12');
    +---------------------------------------+
    | EXTRACT(YEAR_MONTH FROM '2008-09-12') |
    +---------------------------------------+
    |                                200809 |
    +---------------------------------------+
    1 row in set (0.00 sec)
    
    mysql> SELECT EXTRACT(HOUR_MICROSECOND FROM '2008-09-12 12:23:55.023111');
    +-------------------------------------------------------------+
    | EXTRACT(HOUR_MICROSECOND FROM '2008-09-12 12:23:55.023111') |
    +-------------------------------------------------------------+
    |                                                122355023111 |
    +-------------------------------------------------------------+
    1 row in set (0.00 sec)
    

    常见的unit值和其对应的格式如下表:

    unit Value Expected expr Format
    MICROSECOND MICROSECONDS
    SECOND SECONDS
    MINUTE MINUTES
    HOUR HOURS
    DAY DAYS
    WEEK WEEKS
    MONTH MONTHS
    QUARTER QUARTERS
    YEAR YEARS
    SECOND_MICROSECOND 'SECONDS.MICROSECONDS'
    MINUTE_MICROSECOND 'MINUTES:SECONDS.MICROSECONDS'
    MINUTE_SECOND 'MINUTES:SECONDS'
    HOUR_MICROSECOND 'HOURS:MINUTES:SECONDS.MICROSECONDS'
    HOUR_SECOND 'HOURS:MINUTES:SECONDS'
    HOUR_MINUTE 'HOURS:MINUTES'
    DAY_MICROSECOND 'DAYS HOURS:MINUTES:SECONDS.MICROSECONDS'
    DAY_SECOND 'DAYS HOURS:MINUTES:SECONDS'
    DAY_MINUTE 'DAYS HOURS:MINUTES'
    DAY_HOUR 'DAYS HOURS'
    YEAR_MONTH 'YEARS-MONTHS'
  • DATE_ADD(date,INTERVAL expr unit), DATE_SUB(date,INTERVAL expr unit)这两个函数可以对日期进行数学计算。

    date参数指定了起始的日期/日期时间值

    expr参数指定了需要在起始日期的基础上增加/减少的时间区间。expr被视为一个字符串,可以以'-'开始去表示一个负的间隔。

    unit表示expr的格式,标明了expr该以何种方式进行翻译

    返回值可能为:

    1. DATE:当date参数为一个日期表达式,并且计算过程中只涉及了DATE, MONTH, DAY
    2. DATETIME:当date参数为一个日期时间表达式/时间戳,或者unit值使用了HOURS, MINUTES, SECONDS
    3. 除了1,2以外的其他情况均返回字符串。
    mysql> SELECT DATE_ADD('2009-03-11', INTERVAL 1 DAY);
    +----------------------------------------+
    | DATE_ADD('2009-03-11', INTERVAL 1 DAY) |
    +----------------------------------------+
    | 2009-03-12                             |
    +----------------------------------------+
    1 row in set (0.01 sec)
    
    mysql> SELECT DATE_SUB('2009-03-12', INTERVAL 1 YEAR);
    +-----------------------------------------+
    | DATE_SUB('2009-03-12', INTERVAL 1 YEAR) |
    +-----------------------------------------+
    | 2008-03-12                              |
    +-----------------------------------------+
    1 row in set (0.00 sec)
    
    mysql> SELECT DATE_ADD('2009-04-10 19:34:12', INTERVAL '25:48' MINUTE_SECOND);
    +-----------------------------------------------------------------+
    | DATE_ADD('2009-04-10 19:34:12', INTERVAL '25:48' MINUTE_SECOND) |
    +-----------------------------------------------------------------+
    | 2009-04-10 20:00:00                                             |
    +-----------------------------------------------------------------+
    1 row in set (0.00 sec)
    
  • DATEDIFF(expr1,expr2)返回expr1-expr2日期的差值(DAY),expr1expr2可以是日期表达式/日期时间表达式,但是只有日期部分会参与运算。

    mysql> SELECT DATEDIFF('2009-01-01','2008-01-01');
    +-------------------------------------+
    | DATEDIFF('2009-01-01','2008-01-01') |
    +-------------------------------------+
    |                                 366 |
    +-------------------------------------+
    1 row in set (0.00 sec)
    
    mysql> SELECT DATEDIFF('2010-10-01 20:12:34','2010-09-30 12:34:22');
    +-------------------------------------------------------+
    | DATEDIFF('2010-10-01 20:12:34','2010-09-30 12:34:22') |
    +-------------------------------------------------------+
    |                                                     1 |
    +-------------------------------------------------------+
    1 row in set (0.01 sec)
    
  • DATE_FORMAT(date, format)根据format指定的格式返回日期。format是一个我们指定的模版,我们通过使用区分符来指定我们想要的某种特殊格式。

    mysql> SELECT DATE_FORMAT('2000-10-01','%b,%D,%Y');
    +--------------------------------------+
    | DATE_FORMAT('2000-10-01','%b,%D,%Y') |
    +--------------------------------------+
    | Oct,1st,2000                         |
    +--------------------------------------+
    1 row in set (0.00 sec)
    
    mysql> SELECT DATE_FORMAT('2010-08-27','day of the week: %w');
    +-------------------------------------------------+
    | DATE_FORMAT('2010-08-27','day of the week: %w') |
    +-------------------------------------------------+
    | day of the week: 5                              |
    +-------------------------------------------------+
    1 row in set (0.00 sec)
    
    

format是一个包含有区分符(specifier)的字符串,常用的区分符有:

Specifier Description
%a Abbreviated weekday name (Sun..Sat)
%b Abbreviated month name (Jan..Dec)
%c Month, numeric (0..12)
%D Day of the month with English suffix (0th, 1st, 2nd, 3rd, …)
%d Day of the month, numeric (00..31)
%e Day of the month, numeric (0..31)
%f Microseconds (000000..999999)
%H Hour (00..23)
%h Hour (01..12)
%I Hour (01..12)
%i Minutes, numeric (00..59)
%j Day of year (001..366)
%k Hour (0..23)
%l Hour (1..12)
%M Month name (January..December)
%m Month, numeric (00..12)
%p AM or PM
%r Time, 12-hour (hh:mm:ss followed by AM or PM)
%S Seconds (00..59)
%s Seconds (00..59)
%T Time, 24-hour (hh:mm:ss)
%U Week (00..53), where Sunday is the first day of the week; WEEK() mode 0
%u Week (00..53), where Monday is the first day of the week; WEEK() mode 1
%V Week (01..53), where Sunday is the first day of the week; WEEK() mode 2; used with %X
%v Week (01..53), where Monday is the first day of the week; WEEK() mode 3; used with %x
%W Weekday name (Sunday..Saturday)
%w Day of the week (0=Sunday..6=Saturday)
%X Year for the week where Sunday is the first day of the week, numeric, four digits; used with %V
%x Year for the week, where Monday is the first day of the week, numeric, four digits; used with %v
%Y Year, numeric, four digits
%y Year, numeric (two digits)
%% A literal % character
%*x* x, for any “x” not listed above
  • ADDDATE(date,INTERVAL expr unit), ADDDATE(expr, days)

    当使用INTERVAL作为第二个参数时,ADDDATE()DATE_ADD()的用法是相同的。

    当使用days作为第二个参数时,MySQL会把days当作一个整数的天数添加到expr

    mysql> SELECT ADDDATE('2011-10-01', 20);
    +---------------------------+
    | ADDDATE('2011-10-01', 20) |
    +---------------------------+
    | 2011-10-21                |
    +---------------------------+
    1 row in set (0.00 sec)
    
  • ADDTIME(expr1, expr2)

    expr1是一个时间表达式/日期时间表达式

    expr2是一个时间表达式

    ADDTIME()会在expr1的基础上加上expr2,并返回结果

    mysql> SELECT ADDTIME('2001-01-01 16:19:29', '00:10:01.1111');
    +-------------------------------------------------+
    | ADDTIME('2001-01-01 16:19:29', '00:10:01.1111') |
    +-------------------------------------------------+
    | 2001-01-01 16:29:30.111100                      |
    +-------------------------------------------------+
    1 row in set (0.00 sec)
    
  • DAY(date)DAYOFMONTH()的同义词,返回date中的日期是该月的哪一天,范围1~31。

    mysql> SELECT DAY('2018-04-12 18:45:26');
    +----------------------------+
    | DAY('2018-04-12 18:45:26') |
    +----------------------------+
    |                         12 |
    +----------------------------+
    1 row in set (0.00 sec)
    
  • YEAR()返回日期所在的年份,范围1000~9999。

    mysql> SELECT YEAR('1999-03-04');
    +--------------------+
    | YEAR('1999-03-04') |
    +--------------------+
    |               1999 |
    +--------------------+
    1 row in set (0.00 sec)
    

常用的文本处理函数

  • LEFT(str,len)返回str字符串中最左侧的len个字符,如果参数为NULL则返回NULL

    mysql> SELECT LEFT('lifetime',4);
    +--------------------+
    | LEFT('lifetime',4) |
    +--------------------+
    | life               |
    +--------------------+
    1 row in set (0.01 sec)
    
  • RIGHT(str,len)返回str字符串中最右侧的len个字符,如果参数为NULL则返回NULL

    mysql> SELECT RIGHT('lifetime',4);
    +---------------------+
    | RIGHT('lifetime',4) |
    +---------------------+
    | time                |
    +---------------------+
    1 row in set (0.00 sec)
    
  • LENGTH(str)返回str字符串的长度,以字节计算,一个多字节字符会按照多个字节计算长度。而当使用CHAR_LENGTH()时,会将一个多字节字符以一个字节计算长度,即长度为1。

    mysql> SELECT LENGTH('lifetime');
    +--------------------+
    | LENGTH('lifetime') |
    +--------------------+
    |                  8 |
    +--------------------+
    1 row in set (0.00 sec)
    
    mysql> SELECT LENGTH('生活');
    +------------------+
    | LENGTH('生活')   |
    +------------------+
    |                6 |
    +------------------+
    1 row in set (0.00 sec)
    
    mysql> SELECT CHAR_LENGTH('生活');
    +-----------------------+
    | CHAR_LENGTH('生活')   |
    +-----------------------+
    |                     2 |
    +-----------------------+
    1 row in set (0.00 sec)
    
    
  • LOWER(str)根据目前的字符集将str中的所有字符转换为小写并返回。

    mysql> SELECT LOWER('HELLO');
    +----------------+
    | LOWER('HELLO') |
    +----------------+
    | hello          |
    +----------------+
    1 row in set (0.00 sec)
    
  • UPPER(str)根据目前的字符集将str中的所有字符转换为大写并返回。

    mysql> SELECT UPPER('hello');
    +----------------+
    | UPPER('hello') |
    +----------------+
    | HELLO          |
    +----------------+
    1 row in set (0.00 sec)
    

    注意:LOWER()UPPER()对二进制字符串无效(BINARY,VARBINARY,BLOB)。转换大小写时需要将其转换为非二进制字符串。

  • LTRIM(str)str开头处的空格移除,返回移除空格后的字符串。该函数可以用于包含多字节字符的字符串。

    mysql> SELECT LTRIM('    shop');
    +-------------------+
    | LTRIM('    shop') |
    +-------------------+
    | shop              |
    +-------------------+
    1 row in set (0.00 sec)
    
    mysql> SELECT LTRIM('    商店');
    +---------------------+
    | LTRIM('    商店')   |
    +---------------------+
    | 商店                |
    +---------------------+
    1 row in set (0.00 sec)
    
  • RTRIM(str)将`str尾部的空格移除,返回移除空格后的字符串。该函数可以用于包含多字节字符的字符串。

    mysql> SELECT LTRIM('shop    ');
    +-------------------+
    | LTRIM('shop    ') |
    +-------------------+
    | shop              |
    +-------------------+
    1 row in set (0.00 sec)
    
    mysql> SELECT LTRIM('商店    ');
    +---------------------+
    | LTRIM('商店    ')   |
    +---------------------+
    | 商店                |
    +---------------------+
    1 row in set (0.00 sec)
    
  • TRIM([{BOTH | LEADING | TRAILING} [remstr] FROM] str),TRIM([remstr FROM] str)

    str中所有的remstr前缀/后缀移除。如果没有使用BOTH,LEADING,TRAILING这些区分符,会假定使用BOTH

    remstr是可选的,如果没有指定,则会移除空格。

    mysql> SELECT TRIM('   test    ');
    +---------------------+
    | TRIM('   test    ') |
    +---------------------+
    | test                |
    +---------------------+
    1 row in set (0.00 sec)
    
    mysql> SELECT TRIM(LEADING '-' FROM '---author---');
    +---------------------------------------+
    | TRIM(LEADING '-' FROM '---author---') |
    +---------------------------------------+
    | author---                             |
    +---------------------------------------+
    1 row in set (0.00 sec)
    
    mysql> SELECT TRIM(TRAILING '-' FROM '---author---');
    +----------------------------------------+
    | TRIM(TRAILING '-' FROM '---author---') |
    +----------------------------------------+
    | ---author                              |
    +----------------------------------------+
    1 row in set (0.00 sec)
    
    mysql> SELECT TRIM(BOTH '-' FROM '---author---');
    +------------------------------------+
    | TRIM(BOTH '-' FROM '---author---') |
    +------------------------------------+
    | author                             |
    +------------------------------------+
    1 row in set (0.00 sec)
    
    mysql> SELECT TRIM('-' FROM '---author---');
    +-------------------------------+
    | TRIM('-' FROM '---author---') |
    +-------------------------------+
    | author                        |
    +-------------------------------+
    1 row in set (0.00 sec)
    
  • SUBSTRING(str, pos),SUBSTRING(str FROM pos),SUBSTRING(str,pos,len),SUBSTRING(str FROM pos FOR len)返回截取后的字符串。

    1. 参数列表中没有len参数时,会返回一个从pos位置开始的字符串。
    2. 参数列表中有len参数时,会返回一个从pos位置开始,长度为len的字符串。
    3. 使用FROM的是标准写法。
    4. pos值为负数时,表示该位置是从字符串尾部开始计算的。
    5. len小于1时,返回一个空的字符串
    mysql> SELECT SUBSTRING('www.google.com', 5);
    +--------------------------------+
    | SUBSTRING('www.google.com', 5) |
    +--------------------------------+
    | google.com                     |
    +--------------------------------+
    1 row in set (0.01 sec)
    
    mysql> SELECT SUBSTRING('www.google.com' FROM 5);
    +------------------------------------+
    | SUBSTRING('www.google.com' FROM 5) |
    +------------------------------------+
    | google.com                         |
    +------------------------------------+
    1 row in set (0.00 sec)
    
    mysql> SELECT SUBSTRING('www.google.com',5,6);
    +---------------------------------+
    | SUBSTRING('www.google.com',5,6) |
    +---------------------------------+
    | google                          |
    +---------------------------------+
    1 row in set (0.00 sec)
    
    mysql> SELECT SUBSTRING('www.google.com'FROM 5 FOR 6);
    +-----------------------------------------+
    | SUBSTRING('www.google.com'FROM 5 FOR 6) |
    +-----------------------------------------+
    | google                                  |
    +-----------------------------------------+
    1 row in set (0.00 sec)
    
    mysql> SELECT SUBSTRING('www.google.com',-3,3);
    +----------------------------------+
    | SUBSTRING('www.google.com',-3,3) |
    +----------------------------------+
    | com                              |
    +----------------------------------+
    1 row in set (0.00 sec)
    

你可能感兴趣的:(MySQL常用日期处理函数和文本处理函数)