可以用substring截取,也可以转换成datetime然后用year、month、day三个函数计算
以标准日期格式2012-12-19
10:50:02000为例
substring(col,1,4)=year
substring(col,6,2)=month
substring(col,9,2)=day
year(convert(datetime,col))、month(convert(datetime,col))、day(convert(datetime,col))
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 (000 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 (000 sec)
以上就是关于从sqlserver数据库中提取日期,并把年月日分别截取出来全部的内容,包括:从sqlserver数据库中提取日期,并把年月日分别截取出来、那个在 my sql 中 我如果只要获取表中时间一列中的具体的年份和月份,应该怎么写啊 比如说 我就只要 2004、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)