Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL - Date Column Format

I am familiar with DATE_FORMAT function which can display the the record from my date field in the format specified. However, I would like to create a table with a date field that only accepts my format.

Here's what I have done so far:

CREATE TABLE test_table (
id INT AUTO_INCREMENT, 
f_name VARCHAR(40) NOT NULL, 
l_name VARCHAR(25) NOT NULL, 
date_hired DATE NOT NULL
);

Inserting a record with a date_hired value of '2013-03-01' will be inserted as '1/03/2013 12:00:00 AM' which is far from my expected result (I would like the format the way it was inserted). Any feedback? Did I miss something?

Thanks,
Michael

like image 872
Michael 'Maik' Ardan Avatar asked Jun 18 '13 07:06

Michael 'Maik' Ardan


People also ask

How can get date in dd mm yyyy format in MySQL?

The following is the output. The following is the query to format the date to YYYY-MM-DD. mysql> select str_to_date(LoginDate,'%d. %m.

Is there a date data type in MySQL?

MySQL comes with the following data types for storing a date or a date/time value in the database: DATE - format YYYY-MM-DD. DATETIME - format: YYYY-MM-DD HH:MI:SS. TIMESTAMP - format: YYYY-MM-DD HH:MI:SS.

How are dates represented in MySQL?

MySQL recognizes DATE values in these formats: As a string in either ' YYYY-MM-DD ' or ' YY-MM-DD ' format. A “relaxed” syntax is permitted: Any punctuation character may be used as the delimiter between date parts. For example, '2012-12-31' , '2012/12/31' , '2012^12^31' , and '2012@12@31' are equivalent.


1 Answers

You can't change the format during table create, you can change the format of date for displaying user by using you programming logic like if you are using PHP as your server side language the you can convert it your desired format.

like image 69
kundan Avatar answered Oct 06 '22 23:10

kundan