Differences

This shows you the differences between two versions of the page.

oracle-to-mariadb:to_date [December 21, 2017 7:03 am]
sqlines
oracle-to-mariadb:to_date [December 21, 2017 7:13 am] (current)
sqlines
Line 57: Line 57:
  SELECT TO_DATE('21-DEC-17') FROM dual;   SELECT TO_DATE('21-DEC-17') FROM dual;
  # 21-DEC-17   # 21-DEC-17
 +</code>
 +
 +In MariaDB, for some date formats you can also use CAST AS DATETIME function without specifying the date format, for other format you have to use STR_TO_DATE function and specify the format explicitly:
 +
 +**MariaDB**:
 +
 +<code sql>
 +  -- MariaDB will not cast this string to datetime
 +  SELECT CAST('21-DEC-17' AS DATETIME);
 +  # NULL
 +  
 +  -- So you have to specify the format
 +  SELECT STR_TO_DATE('21-DEC-17', '%d-%b-%y');
 +  # 2017-12-21
 +  
 +  -- But the following formats are recognized:
 +  SELECT CAST('2017-12-21' AS DATETIME);
 +  # 2017-12-21 00:00:00
 +  
 +  SELECT CAST('20171221' AS DATETIME);
 +  # 2017-12-21 00:00:00
 +  
 +  SELECT CAST('2017/12/21' AS DATETIME);
 +  # 2017-12-21 00:00:00
 +  
 +  SELECT CAST('2017.12.21' AS DATETIME);
 +  # 2017-12-21 00:00:00
</code> </code>