2017-04-14 2 views
-1

Ich habe einige TeildatenMySQL Teildatum in Datetime-Spalte

wie 2017-12 (Y-M) oder 2017-45 (Y-W)

ist es möglich, sie in einer Datetime-Spalte in MySQL zu speichern?

+0

Mögliche Duplikat von [Wie Datetime zu VarChar konvertiert] (http://stackoverflow.com/ questions/74385/how-to-convert-datetime-to-varchar) –

+0

http://stackoverflow.com/questions/74385/how-to-convert-datetime-to-varchar –

+0

Hinzufügen des ersten Tages des Monats oder der erster Tag der Woche – etsa

Antwort

0

Verwendung STR_TO_DATE() Funktion wie folgt aus:

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; 

Probe

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>