我有一些部分日期
如2017-12(Y-M) 或2017-45(Y-W)
是否可以将其存储在MySQL的日期时间列中?
答案 0 :(得分:0)
使用 STR_TO_DATE()这样的功能:
SELECT STR_TO_DATE(CONCAT('2017-12','-01'),'%Y-%m-%d');
SELECT STR_TO_DATE(CONCAT(SUBSTRING_INDEX('2017-45', '-', 1),'-01-01'),'%Y-%m-%d')
+ INTERVAL SUBSTRING_INDEX('2017-45', '-', -1) -1 WEEK;
<强>样品强>
mysql> SELECT STR_TO_DATE(CONCAT('2017-12','-01'),'%Y-%m-%d');
+-------------------------------------------------+
| STR_TO_DATE(CONCAT('2017-12','-01'),'%Y-%m-%d') |
+-------------------------------------------------+
| 2017-12-01 |
+-------------------------------------------------+
1 row in set (0,00 sec)
mysql> SELECT STR_TO_DATE(CONCAT(SUBSTRING_INDEX('2017-45', '-', 1),'-01-01'),'%Y-%m-%d')
-> + INTERVAL SUBSTRING_INDEX('2017-45', '-', -1) -1 WEEK;
+------------------------------------------------------------------------------------------------------------------------------------------------+
| STR_TO_DATE(CONCAT(SUBSTRING_INDEX('2017-45', '-', 1),'-01-01'),'%Y-%m-%d')
+ INTERVAL SUBSTRING_INDEX('2017-45', '-', -1) -1 WEEK |
+------------------------------------------------------------------------------------------------------------------------------------------------+
| 2017-11-05 |
+------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0,01 sec)
mysql>