Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change default date format when creating table in MYSQL

Tags:

mysql

how to change default date format when creating table in MYSQL

like image 281
bhaskar Avatar asked Jul 28 '10 06:07

bhaskar


People also ask

How do I change the default date format in MySQL?

Change the curdate() (current date) format in MySQL The current date format is 'YYYY-mm-dd'. To change current date format, you can use date_format().

How do you create a date format in SQL while creating a table?

SQL Date Data TypesDATE - format YYYY-MM-DD. DATETIME - format: YYYY-MM-DD HH:MI:SS. TIMESTAMP - format: YYYY-MM-DD HH:MI:SS. YEAR - format YYYY or YY.

What is the default format for date datatype in the table?

DATE values have YYYY-MM-DD format, where YYYY represent the full year (4 digits), whereas the MM and DD represents the month and day parts of the date respectively (2 digits with leading zero).

How do you set a default value for a MySQL date column?

Learn MySQL from scratch for Data Science and Analytics We can set the now() function as a default value with the help of dynamic default. First, we will create a table with data type” datetime”. After that, we will set now() as the default value for column “MyTime” as shown below.


2 Answers

You can't change the default format for a date during the table definition stage. (It must always obey the DATETIME, DATE or TIMESTAMP formats.) As the manual puts it:

Although MySQL tries to interpret values in several formats, dates always must be given in year-month-day order (for example, '98-09-04'), rather than in the month-day-year or day-month-year orders commonly used elsewhere (for example, '09-04-98', '04-09-98').

See the date and time reference docs for more info.

As such, you'll have to use the DATE_FORMAT() function at the point of output to achieve this goal.

like image 177
John Parker Avatar answered Sep 27 '22 16:09

John Parker


You may want to use the STR_TO_DATE() and DATE_FORMAT() functions to communicate with MySQL using different date formats.

Example using STR_TO_DATE():

SELECT STR_TO_DATE('15-Dec-09 1:00:00 PM', '%d-%b-%y %h:%i:%S %p') AS date;
+---------------------+
| date                |
+---------------------+
| 2009-12-15 13:00:00 |
+---------------------+
1 row in set (0.07 sec)

Example using DATE_FORMAT():

SELECT DATE_FORMAT('2009-12-15 13:00:00', '%d-%b-%y %h:%i:%S %p') AS date;
+-----------------------+
| date                  |
+-----------------------+
| 15-Dec-09 01:00:00 PM |
+-----------------------+
1 row in set (0.00 sec)
like image 45
Daniel Vassallo Avatar answered Sep 27 '22 15:09

Daniel Vassallo