0

How to convert date format's in MySQL.

Databases

As you can see in my Blog entry on how to import a Microsoft Access Database to MySQL , I ran into a small problem on converting all the dates from the format of mm/dd/yyyy to MySQL Date Format of yyyy-mm-dd.

Thanks to all the help I recieved from Guelphad on the MySQL Newbie forum in this post, I got all the dates converted and am good shape.

 Here's how I converted the date formats on Guedlphad's Advice.

 In my table(misequip) , I created a new column with a data type of DATE with a name (dt_deployed) similar to the column that contained my dates imported from Access (date_deployed) in string format.  If you'll remember, I imported all the dates as a datatype of VARCHAR just to get them into the database.

I then ran this command:

mysql> UPDATE misequip SET dt_deployed =  str_to_date(date_deployed, '%m/%d/%Y');

What that command does is UPDATE's the misequip table, SET's the new column dt_deployed to equal the out put of the MySQL function STR_TO_DATE that converts Strings to Dates.

Once I ran this command, I looked at the my database and all the dates were converted.  Worked like a champ.  I then dropped my original column, then renamed my new column to match the old name, date_deployed.

 We will now be albe to do calculations on these dates now since they are in DATE format to figure out when warranties expire and stuff like that.

 Hope this help some of you newbieMySQL guys out there,

Mark

 

  

tags:
Databases
Melle said:
 
thank you so much for a helpful blog. Got my probs in converting dates solved
 
posted 458 days ago
Add Comment Reply to: this comment OR this thread
 
WOW gold said:
 
 
posted 213 days ago
Add Comment Reply to: this comment OR this thread
 
Korey said:
 
Ah, perfect, just what I was looking for. Most data that I download from financial institutions has dates in the mm/dd/yyyy format, so I will definitely have to put this in my nifty-one-liners list. Yes, I use MySQL for personal finance. Wow, I'm geeky. Thanks for the post!
 
posted 63 days ago
Add Comment Reply to: this comment OR this thread
 

Search