How to import Excel's serialized datetime into MySQL - mysql

How do I import and preserve date format fields such as "8/21/2012" from Excel to MySQL?
I am using MySQL Workbench and the Excel MySQL Excel data transfer plug in. When I select the Excel data I want to import into MySQL, I get a window where I declare variable types for all fields. All fields and declarations work as expected except for date and time fields. Both date and time switch from 8/21/2012 to a number like 398475 etc. How can I import these fields into MySQL by preserving the dashed mm/dd/yyyy format? I assume the same procedure will work for time as well.
Alternatively, is there a way to convert the serialized datetime value (a float, representing the number of days since 1/1/1900) back to mm/dd/yyyy in MySQL ?
Thank you!

You can convert your date cells into a MySQL supported format using the TEXT function.
=TEXT(A1,"YYYY-MM-DD")
This will convert a date in cell A1 to the yyyy-mm-dd format that the MySQL date field expects.

To get the serialized date format (say 36422) into a useful date format in MYSQL use the interval function added to the base date that Excel uses (which is actually 1900-00-00 but since that doesn't exist you will have to use 1900-01-01 which is why we subtract 2 from the date column)
`'1900-01-01' + INTERVAL(Your_Date_Column - 2)DAY`

You can convert your datetime cells into a MySQL supported format using the TEXT function
=TEXT(A1,"YYYY-MM-DD HH:MM:SS")

Simply use Date (java.util package) for this.
Date d=Date date=cell.getDateCellValue();
Now Use it as you want.

Related

how to change the date format in excel to text so it can convert in mysql

I have data in excel in which there is a column date formatted dd/mm/yyyy.
that excel data I want to convertted to mysql.
the problem is after converting this, mysql read as 0000-00-00.
I used formula =TEXT(A1,"YYYY-MM-DD") but the excel ask for punctuation mark (') in front of sign equal to (=)
I change the date to 'YYYY-MM-DD and mysql success to read the date.
What exactly the best formula I used to apply to all (if more than 200) date so it can read by mysql.
thanks
This should do the trick:
Highlight the column
Right click and select Format Cells
Select Custom
In the Type field enter yyyy-mm-dd
Now your dates should be in the correct format.

Importing .csv and converting to dates in mysql

I am trying to import a CSV file with date information in the format MMM-YY (e.g., Jan-92). My ultimate goal is to have the information in a date format in SQL. The format doesn't matter that much to me, but I was thinking something like dd-mm-yy (e.g., 16-01-92). I've tried a lot and looked around the forum, but can't figure it out.
Right now, I am loading the data from the CSV into a column "period_month" as a VARCHAR field.
Then, my conversion code looks like
UPDATE PERIODS
SET period_month = DATE(str_to_date(period_month,'%M-%Y'));
I end up with a field of VARCHAR type in the format yyyy-mm-dd, with the dd field set to '00'. (e.g., 1992-01-00)
I really just want this to be in a date format that I can export to another program like R for analysis.
Any help appreciated.
try
UPDATE PERIODS
SET period_month =DATE(str_to_date('Jan-92','%M-%Y')+1)
or
UPDATE PERIODS
SET period_month =DATE(str_to_date('period_month','%M-%Y')+1)
use date_format function of mysql query

Change Date Format in SQL Server 2008

I have a table where users used to enter random date formats like
dd/MM/Year or MM/dd/Year.
But I want to update all those dates to this format Year/MM/dd
I have made a cursor and changed data by CHAR INDEX and sub string..
but is there any easy way to change the date format to the one i want ?
and also how can I make my default database date format = Year/mm/dd
only database not server?
You can convert it into different styles.
for example:
select convert(varchar(15),getdate(),103)
will give today's date in dd/mm/yyyy.
Full list of converting styles supported by MSSQL is here

Convert varchar string data to time format in mysql

I need to convert varchar string data to time format in mysql.
For example,
I have a varchar column in table which stores time. The accepted values should be like 9:30 AM or 1200 PM. But currently it has either blank values or it has values like 9.30am or 12:00
There are many records like this, so cannot update manually.
Ithere any work around or function or procedure to do so?
please help.
Thanks
You can use the STR_TO_DATE() MySQL function to convert any string to a date.
Additionally you can use TIME() to extract the time portion of a datetime. A combination of both function is used to convert an arbitrary date string to a datetime and then you can extract the time portion from it as a valid MySQL TIME.
By default MySQL functions follow standard format but custom format can be specified and if your values don't use the international formats you'll need to check with the documentation and provide the format your system is using.

How to make the default date format dd/mm/yyyy in SQL Server 2008?

I have an Excel file that contains a column full of dates in the dd/mm/yyyy format. When I try to import it using openrowset, it said that there was a datatype mismatch. I have a table where the date is defined as type date. Now, I know that the default date format in SQL Server is yyyy-mm-dd. How can I avoid this conflict? Is there a way I can make the default date type be dd/mm/yyyy? I need to do this import operation everyday and it has to be automated and so I cannot afford it to fail in between. I tried using sp_addlanguage to make it British as the default date type is dd/mm/yyyy there, but it didn't work :(. I'm using SQL Server 2008 and Windows 7, if that is of any help. Please help me out! Thanks!
You could CONVERT the incoming data before you insert it. So, in the openrowset statement, where you select the field, you could surround it with a CONVERT statement. Here's an example:
print convert(date,'19/07/2010',103)
This is a UK style date, but if you run it you can see that it's converted it to SQL-friendly format.