i have some partial dates
like 2017-12 (Y-M) or 2017-45 (Y-W)
is it possible to store it in a datetime column in MySQL ?
i have some partial dates
like 2017-12 (Y-M) or 2017-45 (Y-W)
is it possible to store it in a datetime column in MySQL ?
 
    
    use STR_TO_DATE() function like this:
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;
sample
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>
