Convert a Unicode time in a CSV spreadsheet so I can import to MySQL date/time stamp - mysql

I'm stuck on this. I have a guestbook I'm converting from an old website into Wordpress comments. The guestbook has all the dates written in as unix. I already have the file transposed so I can import it into Wordpress comments and all works good, except the date field, which defaults to 0000-00-00 00:00:00.
I also have fields in standard Excel time formats but I need to find a way to conver that to the standard 0000-00-00 00:00:00.
Any ideas?

I actually figured this out after posting. Here was the issue. I had a CSV file from a previous guestbook that contained comments. In that CSV file the date / timestamp was in UNIX code (i.e. seconds since 1/1/1970).
In Excel, I found a formula to convert that to a date Excel can read. However, that date, when opened as a CSV, doesn't show in the correct format for MySQL. It uses the Excel date format, which is some string of numbers, I"m assuming similar to Unix time - from a certain date.
What I did was use the excel formula TEXT(Value, YYYY-MM-DD HH:MM:SS) to convert the excel date/time to a string in the correct format. Then the CSV file worked.

Related

In MYSQL, how to upload a csv file that contains a date in the format of '1/1/2020' properly into a DATE data type format (standard YYYY-MM-DD)

I have a column of data, let's call it bank_date, that I receive from an external vendor as a csv file every day. As such the dates in that column show as '1/1/2020'.
I am trying to upload that raw csv file directly to SQL daily. We used to store the SQL bank_date format as text, but we have converted it to a Data data type, and now it keeps zero'ing out every time, with some sort of truncate / "datetime value incorrect" error.
I have now tested 17 different versions of utilizing STR_TO_date (mostly), CAST, and CONVERT, and feel like I'm close, but I'm not quite getting the syntax right.
Also for reference, I did find 2 other workarounds that are successful, but my boss specifically wants it uploaded and converted directly through the import process (not manipulating the raw csv data) for safety reasons. For reference:
Workaround 1: Convert csv date column to the YYYY-MM-DD format and save file. The issue with this is that if you try to open that CSV file again, it auto-changes the date format back to the standard mm/dd/yyyy. If someone doesn't know to watch out for this and is re-opening the csv file to double check something, they're gonna find an error when they upload, and the problem is not easy to identify.
Workaround 2:Create an extra dummy_date column in the table that is formatted as a text data type and upload as normal. Then copy and paste the data into the correct bank_date column using a str_to_date function as follows: UPDATE dummy_date SET bank_date = STR_TO_DATE(dummy_date, ‘%c/%e/%Y’); The issue with this is that it just creates extra unnecessary data that can be confused when other people may not know that 1 of the columns is not intended for querying.
Here is my current code:
USE database_name;
LOAD DATA LOCAL INFILE 'C:/Users/Shelly/Desktop/Date Import.csv'
INTO TABLE bank_table
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 1 ROWS
(bank_date, bank_amount)
SET bank_date = str_to_date(bank_date,'%Y-%m-%d');
The "SET" line is what I cannot work out on syntax to convert a csv's 1/5/2020' to SQL's 2020-1-5 format. Every test I've made either produces 0000-00-00 or nulls the column cells. I'm thinking maybe I need to tell SQL how to understand the csv's format in order for it to know how to convert it. Newbie here and stuck.
You need to specify a format for a date that is in the file, not a "required" one:
SET bank_date = str_to_date(bank_date,'%c/%e/%Y');

Time field (in the format '02:00:00 PM' ) not able to export from Excel to MySql

I am trying to export data from excel(which I downloaded form a different source) to MySql using 'MySql for Excel' plugin, but I am facing issue with the time field. The format of the time is out of my control. I've tried with both formats(General and Time) in Excel. Still seeing this issue. Is there workaround for this?
Export it as a string, not a TIME.
in LOAD DATA, import it as a VARCHAR() into an #variable
use str_to_date() to parse it into the TIME column.

select * INTO outfile generates date in wrong format

I am using the below sql to export data to a csv file:
select * INTO outfile 'customer.csv'
FIELDs terminated by ',' enclosed by '"'
LINES terminated by '\n'
from CUSTOMER customer
where customer.date_created < '2015-10-22 10:00:00';
I get this result in csv:
Problem is data doesn't import from this generated csv because the date format is different than in DB.
DB date format is yyyy-mm-dd hh:mm:ss. Also null values are replaced with \n which also fail when importing.
How can I generate the csv columns with correct i.e. yyyy-mm-dd hh:mm:ss date format and null/empty values?
Errors:
Incorrect datetime value: '23/07/2015 11:55' for column 'DATE_CREATED' at row 1
Incorrect integer value: '\N' for column 'column_name' at row 1
Note:
I am using mysql workbench to import the file.
I don't want to change the format/data directly in csv file.
Thanks
UPDATE:
Thanks to AdrianBR I realised I was opening the file with excel first which was overriding the date format hence wrong date format was showing even with notepad++.
\n is still a problem.
When opened with notepad++ for the first time it looks like this:
"100","0","2015-12-02 10:16:36","2015-12-02 10:16:36","0",\N,
The issue is most likely not coming from mysql.
It is most likely coming from the way Excel displays and later saves the dates.
to troublehsoot:
Open the file in a text editor such as notepad or notepad++ and check what the date looks like, if it's in ISO or not. It will probably be fine.
Now, if you open it in excel, it will be displayed in local format.
If you save the file now, you are likely to overwrite the ISO date format, with excel's local date format, making it not a valid importable mysql date anymore.
Moral: don't use excel when working with data, only use it to display charts. Excel makes assumptions about your data and messes with it in the most unexpected ways. Remember than 1.19 VAT tax rate? Excel seems to think it's the same as Jan 19. That integer ID? Excel thinks it's better off to write it in scientific notation and round it to first 4 digits. That Iso date? Excel thinks you are better off guessing which is the month and which is the date. That decimal point? surely you wanted comma as decimal, and dot as thousands separator instead. FTFY!
maybe specify the columns explicitly, and include the format in the select list like so:
TO_CHAR( mydate, 'yyyy-mm-dd hh24:mi:ss' )
edit:
as in:
SELECT my_id, my_val1, TO_CHAR( mydate, 'yyyy-mm-dd hh24:mi:ss' )
FROM mytable

How to import excel stored data in MySQL and keep date format as yyyy-mm-dd hh:mm:ss

I have come across somewhat difficult task to complete. I have an excel document with various columns and two of them are date columns with values in this format yyyy-mm-dd hh:mm:ss.
I need to import all this data in MySQL and so far tried to do it with a CSV file using phpmyadmin.
The problem is that CSV is changing the date format from yyyy-mm-dd hh:mm:ss to dd/mm/yyyy hh:mm:ss.
What other ways exist to import data originating from excel file into MySQL and keep the data format?
I would appreciate some simple solution if such exists.
Thank you!
I'm not familiar with importing Excel files into MySQL, in SQL Server this is trivial using the import wizard so I'd assume something similar exists for MySQL.
If you need to, you can convert the value to text with the format of your choosing in Excel with the following:
=TEXT(A1,"yyyy-mm-dd hh:MM:ss")
This format will persist in the csv.
Answering my question here - one possibility to export dates from excel into CSV file and keep the date format as yyyy-mm-dd or yyyy-mm-dd hh:mm:ss is to save the file as .ods document - the file format of Open Office.
From the .ods file you can save your document as CSV file and the dates are being kept as yyyy-mm-dd or yyyy-mm-dd hh:mm:ss. For some reason Open Office keeps the selected date format when saving into CSV and Excel does not.
Hope this helps someone.

Date conversion from csv to mysql

I'm trying to import (through phpMyadmin import option) a csv file to mysql table. The CSV file has a column date with the format mm/dd/yyyy (10/21/2009) and the column in mysql table has the date format yyyy-mm-dd. I've tried to change formats in csv but after importing to mysql the date column shows 0000-00-00 or some other wrong dates. Which is the correct date format to be stored in the csv file so that it shows the correct date after importing?
Thanks.
Use a date column for the imported data (i.e. varchar)
Add a column for the new date column (i.e. Date)
Use STR_TO_DATE() to convert.
Drop old date column (optional)
In your case:
UPDATE your_table SET new_date_col = STR_TO_DATE(old_date_col, '%m/%d/%Y')
Adjust the original format to your needs.