Mysql: Setup the format of DATETIME to 'DD-MM-YYYY HH:MM:SS' when creating a table

MysqlDatetimeCommand LineFormatCreate Table

Mysql Problem Overview


After googling around, I cannot find a way to create a new table with a DATETIME column with the default format set to 'DD-MM-YYYY HH:MM:SS'

I saw a tutorial in which it was done in phpmyadmin so I suspect that I could use mysql via command line and achieve the same thing when creating my new table with

CREATE TABLE ()

Thank you in advance

Mysql Solutions


Solution 1 - Mysql

"MySQL retrieves and displays DATETIME values in 'YYYY-MM-DD HH:MM:SS' format." This is from mysql site. You can store only this type, but you can use one of the many time format functions to change it, when you need to display it.

Mysql Time and Date functions

For example, one of those functions is the DATE_FORMAT, which can be used like so:

SELECT DATE_FORMAT(column_name, '%m/%d/%Y %H:%i') FROM tablename

Solution 2 - Mysql

Use DATE_FORMAT function to change the format.

SELECT DATE_FORMAT(CURDATE(), '%d/%m/%Y')

SELECT DATE_FORMAT(column_name, '%d/%m/%Y') FROM tablename

Refer DOC for more details

Solution 3 - Mysql

As others have explained that it is not possible, but here's alternative solution, it requires a little tuning, but it works like datetime column.

I started to think, how I could make formatting possible. I got an idea. What about making trigger for it? I mean, adding column with type char, and then updating that column using a MySQL trigger. And that worked! I made some research related to triggers, and finally come up with these queries:

CREATE TRIGGER timestampper BEFORE INSERT ON table
FOR EACH
ROW SET NEW.timestamp = DATE_FORMAT(NOW(), '%d-%m-%Y %H:%i:%s');

CREATE TRIGGER timestampper BEFORE UPDATE ON table
FOR EACH
ROW SET NEW.timestamp = DATE_FORMAT(NOW(), '%d-%m-%Y %H:%i:%s');

You can't use TIMESTAMP or DATETIME as a column type, because these have their own format, and they update automatically.

So, here's your alternative timestamp or datetime alternative! Hope this helped, at least I'm glad that I got this working.

Solution 4 - Mysql

i have used following line of code & it works fine Thanks.... @Mithun Sasidharan **

> SELECT DATE_FORMAT(column_name, '%d/%m/%Y') FROM tablename

**

Solution 5 - Mysql

I'm pretty certain that you can't change the datetime format in mysql. The phpmyadmin setting is probably applying a custom format as it reads the datetime (using DATE_FORMAT or something from php). It shouldn't matter what format the database uses, format in the application to display it as you wish.

Date formatting is a pretty common task. I typically like to abstract it out into internationalization code or, if you don't need to deal with i18n, into a common date utility library. It helps keep things consistent and makes it easier to change later (or add i18n support).

Solution 6 - Mysql

No you can't; datetime will be stored in default format only while creating table and then you can change the display format in you select query the way you want using the http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html">Mysql Date Time Functions

Solution 7 - Mysql

This cannot be done for the table; besides, you even cannot change this default value at all.

The answer is a server variable datetime_format, it is unused.

Solution 8 - Mysql

Dim x as date 

x = dr("appdate")
appdate = x.tostring("dd/MM/yyyy")

dr is the variable of datareader

Solution 9 - Mysql

try this:

DATE NOT NULL FORMAT 'YYYY-MM-DD'

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionIam ZeshView Question on Stackoverflow
Solution 1 - MysqlMirko AkovView Answer on Stackoverflow
Solution 2 - MysqlMithun SasidharanView Answer on Stackoverflow
Solution 3 - MysqlaksuView Answer on Stackoverflow
Solution 4 - MysqlDatta SalunkheView Answer on Stackoverflow
Solution 5 - MysqlcolithiumView Answer on Stackoverflow
Solution 6 - MysqlRahulView Answer on Stackoverflow
Solution 7 - MysqlDevartView Answer on Stackoverflow
Solution 8 - MysqlynotView Answer on Stackoverflow
Solution 9 - MysqlGigolNftView Answer on Stackoverflow