sql如何获取一列日期的年份和月份

发布网友 发布时间:2022-04-22 22:20

我来回答

3个回答

热心网友 时间:2022-04-08 23:29

mysql> select DATE_FORMAT('1997-10-04 22:23:00', '%Y/%m ');
+----------------------------------------------+
| DATE_FORMAT('1997-10-04 22:23:00', '%Y/%m ') |
+----------------------------------------------+
| 1997/10 |
+----------------------------------------------+

热心网友 时间:2022-04-09 00:47

sqlserver 吗?

select convert(varchar(7),dtcol,111) from A

热心网友 时间:2022-04-09 02:21

WHERE
Year( 订购日期 ) = 2004 AND MONTH ( 订购日期 ) = 5;

就可以了。

注: MySQL 中, 获取 日期的组成部分, 有2套函数可以使用, 下面是执行的例子代码:

mysql> SELECT 'NOW' DisplayMode, NOW() B
-> UNION ALL
-> SELECT 'Second' DisplayMode, Second(NOW()) B
-> UNION ALL
-> SELECT 'Minute' DisplayMode, MINUTE(NOW()) B
-> UNION ALL
-> SELECT 'Hour' DisplayMode, Hour(NOW()) B
-> UNION ALL
-> SELECT 'Day' DisplayMode, DAY(NOW()) B
-> UNION ALL
-> SELECT 'Week' DisplayMode, Week(NOW()) B
-> UNION ALL
-> SELECT 'Month' DisplayMode, Month(NOW()) B
-> UNION ALL
-> SELECT 'Year' DisplayMode, Year(NOW()) B;
+-------------+---------------------+
| DisplayMode | B |
+-------------+---------------------+
| NOW | 2013-04-07 09:29:54 |
| Second | 54 |
| Minute | 29 |
| Hour | 9 |
| Day | 7 |
| Week | 14 |
| Month | 4 |
| Year | 2013 |
+-------------+---------------------+
8 rows in set (0.00 sec)

mysql> SELECT '年' AS name, EXTRACT(YEAR FROM NOW()) AS value
-> UNION ALL
-> SELECT '年月' AS name, EXTRACT(YEAR_MONTH FROM NOW()) AS value
-> UNION ALL
-> SELECT '月' AS name, EXTRACT(MONTH FROM NOW()) AS value
-> UNION ALL
-> SELECT '日' AS name, EXTRACT(DAY FROM NOW()) AS value
-> UNION ALL
-> SELECT '时' AS name, EXTRACT(HOUR FROM NOW()) AS value
-> UNION ALL
-> SELECT '分' AS name, EXTRACT(MINUTE FROM NOW()) AS value
-> UNION ALL
-> SELECT '秒' AS name, EXTRACT(SECOND FROM NOW()) AS value;
+------+--------+
| name | value |
+------+--------+
| 年 | 2013 |
| 年月 | 201304 |
| 月 | 4 |
| 日 | 7 |
| 时 | 9 |
| 分 | 27 |
| 秒 | 43 |
+------+--------+
7 rows in set (0.00 sec)

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com