sql如何获取一列日期的年份和月份
例如,表A中有一列显示日期,格式为:2013/7/1,如何让这一列的值只显示2013/7,O(∩_∩)O谢谢...
例如,表A中有一列显示日期,格式为:2013/7/1,如何让这一列的值只显示2013/7,O(∩_∩)O谢谢
展开
3个回答
展开全部
mysql> select DATE_FORMAT('1997-10-04 22:23:00', '%Y/%m ');
+----------------------------------------------+
| DATE_FORMAT('1997-10-04 22:23:00', '%Y/%m ') |
+----------------------------------------------+
| 1997/10 |
+----------------------------------------------+
+----------------------------------------------+
| DATE_FORMAT('1997-10-04 22:23:00', '%Y/%m ') |
+----------------------------------------------+
| 1997/10 |
+----------------------------------------------+
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
sqlserver 吗?
select convert(varchar(7),dtcol,111) from A
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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)
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)
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询