Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Import excel file into mySQL with correct format

Posted on 2011-09-14
14
Medium Priority
?
1,377 Views
Last Modified: 2012-06-27
Hello!

Was wondering - well hoping one you guys will be able to help me out?!

I'm trying to add data from an excel file to my DB in mySQL (xampp).

I have managed to add data to all of my tables except one. The data is added when I import it, only problem is the one column is date format - but once its in mySQL it is shown as a integer.
e.g. 28/03/2006 in excel and then once imported to mySQL its shown as 38804.00000

I have tried to change the structure of the table  - change the integer field to date format - but that ends up changing all the values under that column to 0000-00-00

Hope one of you guys can help out!
Chelsea
0
Comment
Question by:h3rm1t9536
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 5
14 Comments
 
LVL 9

Expert Comment

by:jeremycrussell
ID: 36536674
You will have to set that column as a varchar or text type to be able to import a date formatted like that.
0
 

Author Comment

by:h3rm1t9536
ID: 36536697
Image
0
 

Author Comment

by:h3rm1t9536
ID: 36536736
If I change the format type to text in excel the values are changed to integers
eg. 28/03/2006 is shown as 38804

I dont mind how the date is displayed as long as it is a date and not a integer...
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

Assisted Solution

by:rmnanney
rmnanney earned 1000 total points
ID: 36536766
1. You can transform the date in excel with this formula =MID(N1,7,4)&"-"&MID(N1,4,2)&"-"&MID(N1,1,2) in a temporary column.

2. Save the file as a CSV, exit Excel.

3. Open the CSV, cut the values from your temporary column and paste them over your dates in column 'N'

4. Save once more and import.
0
 

Author Comment

by:h3rm1t9536
ID: 36536924
Hi rmnanney - sounds great but could you please explain a little more what you mean?

Must I copy the formula to each cell in a new column? I have 14000 rows - is it possible to drag and copy (only asking cause I noticed the value changes if I do it that way) or must i do a special paste > value for a whole new column?

Thanks for your help!
0
 

Assisted Solution

by:rmnanney
rmnanney earned 1000 total points
ID: 36536994
What you want to do is copy that formula (from here) and then click the first cell in your Excel file, let's say column O (really you can choose any column you wish, but it should not contain any of your data)

Next, scroll all the way down to your last row in the file (your last row in column N that has data) hold down the shift key and click the last cell in column O.  This will highlight all the appropriate cells in column O.

Last, you can paste the formula and it should calculate the new dates in column O, appropriate for MySQL import.

This should satisfy step 1 in my earlier comment and you can proceed into step 2.  I strongly suggest you make a backup of your original file just for safety's sake.

I hope this helps,
Ryan
0
 

Author Comment

by:h3rm1t9536
ID: 36542188
Hi Ryan

Thanks for explaining... I managed to get to step 3 but this is what the data looked it... not sure if that's what its suppose to look like?
 both columns
Also what type of cvs do I save the file as csv (comma delimited) ?

Also when I try to import the csv file xampp asks a few questions - how do I know which ones to select?
 options
0
 

Author Comment

by:h3rm1t9536
ID: 36542194
sorry included the wrong image should be this one After Step 3
0
 

Accepted Solution

by:
rmnanney earned 1000 total points
ID: 36543316
Couple of things to switch up at this point.  

Be sure to save the file as a comma delimited CSV
When trying to import your data in with PHPMyAdmin, switch the "Fields Terminated By" to a comma ',' instead of a semicolon ';'

With regards to your latter screenshot of the excel file, try to format column Q as text.  I have attached a screenshot to illustrate what you want to see when starting step 3.   The format you are after is YYYY-MM-DD for MySQL.

Screenshot of Excel
If you would like to attach the xls file (say with only your dates) I will happily setup the formula and column formatting for you, as an example.  Actually, now that I have typed (thought about it) I have proactively attached a small example for you.  :)

Cheers,
Ryan
Book1.xlsx
0
 

Author Comment

by:h3rm1t9536
ID: 36559533
Hi Ryan

Thanks so much for all your help! managed to get all the dates in text format dd-mm-yyyy only issue is when I import the data into MySQL it doesn't format that column as 'DATE' format it views it as VAR CHAR 10.

So the display is perfect with VAR CHAR format only problem is I would like it to be in the correct form 'DATE' When I convert/change the format of OrderDate it is unable to show the correct dates as the picture below shows... MySQL screen shots
The format in excel column 'P' was set to 'Text' before I imported the data to MySQL. I also tried to set the data in excel column 'P' with format 'Date' and then tried to import that to MySQL - but both didn't work...

Thanks again for all your help!!
Chelsea
0
 

Assisted Solution

by:rmnanney
rmnanney earned 1000 total points
ID: 36560316
Chelsea,

Looks like you're getting close.  Here's what to do next:

You must have your date format YYYY-MM-DD for importation into MySQL, and save that as text in Excel.

Once you import the CSV file into MySQL, the dates will likely be in VARCHAR() format, as you saw.  The only reason your OrderDate did not convert to DATE() in MySQL is because the OrderDate is DD-MM-YYYY.  Just be sure to get the dates as YYYY-MM-DD and do your import and convert to DATE() again.  It should work for you.

Kind regards,
Ryan
0
 

Author Comment

by:h3rm1t9536
ID: 36560564
Woohoo its sorted!

Ryan thanks so so much for all your help! Your been a star!
0
 

Expert Comment

by:rmnanney
ID: 36560585
Nice!  Glad to see it is working!

Cheers,
Ryan
0
 

Author Closing Comment

by:h3rm1t9536
ID: 36560589
Thanks so much!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
In this article, we’ll look at how to deploy ProxySQL.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question