tags:

views:

186

answers:

5

If I have a date such as mm/dd/yyyy. How can I get mysql to actually store the date in that format. Is this possible or do have to store it in the form of yyyy/mm/dd and convert it later?

What I want to do is insert a date in the mm/dd/yyyy format but the database will not allow it. It wants yyyy/mm/dd

+1  A: 

There is a built-in DATE type in MySQL. It allows to store a date. And later in your programming language you extract this date and convert to whatever format you like. Also, conversion can be done directly in MySQL via DATE_FORMAT(date,format) function.

FractalizeR
Hi, thanks. Are you talking about the data type? I'm aware of that but it takes the yymmdd format. I want to store the date in reverse order.
Can I use date_format for the inserts too? If I can, I'm golden.
There is a DATE_FORMAT(date,format) function to format the date as you like
FractalizeR
What language do you use? Depending on it, you might even don't need to format anything
FractalizeR
I'm using PHP. I've thought about using mktime and storing as a UNIX timestamp but it seems like overkill.
I would so agree with this answer.
Homework
Thanks Joey. I think I agree as well.
You can use STR_TO_DATE with inserts. It is opposite to DATE_FORMAT. I would recommend against using unix timestamps because of their human un-readability.
Josef Sábl
A: 

hi, i would recommend using varchar for that format of yours, however note that sorting would be difficult as your date doesn't start with a year. go for the mysql date format YYYY-MM-DD a convert that to DD/MM/YYYY in your script.

dusoft
A: 

MySQL DATE fields must store the date in YYYY-MM-DD (including dashes) format. If you attempt to store it any other way you will have problems with date comparisons and ordering.

Conversion later is a trivial task. Is there a compelling reason why you are trying to avoid doing this? What other technology are you using to talk to the MySQL database? (i.e. PHP, C# etc...)

Evernoob
+4  A: 

What is your reason for doing this? I can't see any reasonable use for it.

You cannot change the way MySQL stores dates, no. But you can of course format them when reading/writing them. You can do it in SQL query like this:

For example you can use STR_TO_DATE function to format the date when inserting it:

INSERT INTO mytable (mydate) VALUES (STR_TO_DATE('12/31/2009', '%m/%d/%Y'))

And vice versa:

SELECT DATE_FORMAT(mydate, '%m/%d/%Y') FROM mytable /* returns 12/31/2009 */

But as FactalizeR pointed out, it is not a good practice to do it in the query and it should be moved to script, like this (considering you are using PHP).

$date = '12/31/2009';
$date = date('Y-m-d', strtotime($date));
mysql_query("INSERT INTO mytable (mydate) VALUES ({$date})");

And vice versa

$date = mysql_result(mysql_query("SELECT mydate FROM mytable"), 0, 0);
$date = date('m/d/Y', strtotime($date)); //returns 12/31/2009
Josef Sábl
Thanks JS. I tried this and it worked.
Formatting data in SQL queries should be considered a bad practice and should be moved to script.
FractalizeR
Thanks for the remark, I will edit it to the answer.
Josef Sábl
+1  A: 

I think you're mixing up what you are storing - the date itself - with how that date is subsequently referred to.

Why do you want to store in a particular format? Do you want to insert in that format? In which case you might be able to get away with it, depending on the localisation of your install, but otherwise convert - insert would be the way to do it (depending, of course, on how you're inserting).

Are you creating a SQL command from strings?

Unsliced