MONTH()

函数说明

返回日期的月份,对于 1 月到 12 月,返回范围为 1 到 12;对于 “0000-00-00” 或 “2008-00-00” 等日期,返回 0 月份。如果 dateNULL 则返回 NULL

函数语法

  1. > MONTH(date)

参数释义

参数说明
date必要参数。date 参数是合法的日期表达式。

示例

  • 示例 1:
  1. mysql> SELECT MONTH('2008-02-03');
  2. +-------------------+
  3. | month(2008-02-03) |
  4. +-------------------+
  5. | 2 |
  6. +-------------------+
  7. 1 row in set (0.02 sec)
  • 示例 2:
  1. drop table if exists t1;
  2. create table t1 (id int,d date, dt datetime,c char(10),vc varchar(20));
  3. insert into t1 values (1,"2021-01-13", "2021-01-13 13:00:00", "2021-12-15", "2021-12-16");
  4. insert into t1 values (1,"2021-01-31", "2021-01-31 13:00:00", "2021-12-15", "2021-12-16");
  5. insert into t1 values (2,"2022-02-15", "2022-02-15 18:54:29", "2021-02-15", "2021-02-15");
  6. insert into t1 values (2,"2022-02-28", "2022-02-28 18:54:29", "2021-02-15", "2021-02-15");
  7. insert into t1 values (3,"2000-02-29", "2000-02-29 18:54:29", "2021-02-15", "2021-02-15");
  8. insert into t1 values (4,"2023-03-17", "2021-02-17 23:54:59", "2021-03-17", "2021-03-17");
  9. insert into t1 values (5,"1985-04-18", "1985-04-18 00:00:01", "1985-04-18", "1985-04-18");
  10. insert into t1 values (6,"1987-05-20", "1987-05-20 22:59:59", "1987-05-20", "1987-05-20");
  11. insert into t1 values (7,"1989-06-22", "1989-06-22 15:00:30", "1989-06-22", "1989-06-22");
  12. insert into t1 values (8,"1993-07-25", "1987-07-25 03:04:59", "1993-07-25", "1993-07-25");
  13. insert into t1 values (9,"1995-08-27", "1987-08-27 04:32:33", "1995-08-27", "1995-08-27");
  14. insert into t1 values (10,"1999-09-30", "1999-09-30 10:11:12", "1999-09-30", "1999-09-30");
  15. insert into t1 values (11,"2005-10-30", "2005-10-30 18:18:59", "2005-10-30", "2005-10-30");
  16. insert into t1 values (12,"2008-11-30", "2008-11-30 22:59:59", "2008-11-30", "2008-11-30");
  17. insert into t1 values (13,"2013-12-01", "2013-12-01 22:59:59", "2013-12-01", "2013-12-01");
  18. insert into t1 values (14,null, null, null, null);
  19. mysql> select month(d),month(dt) from t1;
  20. +----------+-----------+
  21. | month(d) | month(dt) |
  22. +----------+-----------+
  23. | 1 | 1 |
  24. | 1 | 1 |
  25. | 2 | 2 |
  26. | 2 | 2 |
  27. | 2 | 2 |
  28. | 3 | 2 |
  29. | 4 | 4 |
  30. | 5 | 5 |
  31. | 6 | 6 |
  32. | 7 | 7 |
  33. | 8 | 8 |
  34. | 9 | 9 |
  35. | 10 | 10 |
  36. | 11 | 11 |
  37. | 12 | 12 |
  38. | NULL | NULL |
  39. +----------+-----------+
  40. 16 rows in set (0.01 sec)
  41. mysql> select month(c),month(vc) from t1;
  42. +----------+-----------+
  43. | month(c) | month(vc) |
  44. +----------+-----------+
  45. | 12 | 12 |
  46. | 12 | 12 |
  47. | 2 | 2 |
  48. | 2 | 2 |
  49. | 2 | 2 |
  50. | 3 | 3 |
  51. | 4 | 4 |
  52. | 5 | 5 |
  53. | 6 | 6 |
  54. | 7 | 7 |
  55. | 8 | 8 |
  56. | 9 | 9 |
  57. | 10 | 10 |
  58. | 11 | 11 |
  59. | 12 | 12 |
  60. | NULL | NULL |
  61. +----------+-----------+
  62. 16 rows in set (0.01 sec)