select * INTO outfile generates date in wrong format - mysql

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

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');

String date from a csv file into mysql

I have a csv file I'm trying to parse a bunch of strings and a few are date formats in my database. The code looks a little like this:
load data infile '/temp/db.csv'
into table db.dbtable
fields terminated by '|'
lines terminated by '\n'
ignore 1 rows
(db_id,
dbtable_field1,
db_date,
db_date2)
set
db_date = convert(char(10), Datetime),
db_date2 = convert(char(10), Datetime);
Every time I execute this it comes up with this error:
Error Code: 1292. Incorrect datetime value: '<date time>' for column 'db_date' at row 1
I've been using stackoverflow and other means to try to get this to work, but nothing seems to be working. Am I missing something important here?
--EDIT--
The csv file looks a little something like this:
db_id|dbtable_field1|db_date|db_date2
123|0025AAA|8/12/2014 10:24:32 AM| 8/12/2014 10:24:32 AM
124|0096AB54|2/26/2013|2/26/2013
125|0085ABDE|10/2/2014 4:56:00 PM|10/2/2014 4:56:00 PM
This block isn't exactly my csv file but it's the same format. As you can see the date format changes and thus I cannot use a predefined date format. Currently I am just parsing it as a varchar(25) in order to get it into the database simply, but parsing it into the date for further work would be useful later on (ie. date comparison testing).
I have done this in mutiple ways including:
db_date = str_to_date(#db_date, '%c/%d/%Y %h:%i:%s %p');
and
db_date = convert(char(10), getDate(), 125);
And neither have worked due to the inconsistency in date format.

Import null and improperly formatted datetime values into datetime column MySQL

I'm using a MySQL database with the Sequel Pro interface, and am new to SQL. I'm attempting to import data from a csv file and one of the columns I am importing into is of type datetime. However, the format I receive the data in is mm/dd/yy hh:mm AM/PM or null. Originally, I modified the type of the column to be varchar to avoid the issue but now I need to perform some date functions on the data that can't be done unless the column has a datetime type and format, so I need a way to convert the incoming data to the proper datetime format.
Additionally, people with no knowledge of SQL or databases are going to be running the import statement so it would be preferable to have them simply click file -- import and not have to enter anything complicated into the mysql command line. Also, after running a query I need to export the data in the same format it came in (mm/dd/yy hh:mm AM/PM or null).
Here are some sample values from the column:
Completion Time
null
6/16/14 10:33 AM
null
null
6/16/14 13:03 PM
6/17/14 13:53 PM
6/18/14 14:38 PM
6/18/14 14:52 PM
6/19/14 13:13 PM
6/18/14 18:56 PM
6/18/14 19:02 PM
null
A possibly simple solution that I've gathered might not be such a good idea from a couple of hours of googling, would be to keep the column type as varchar then somehow extract just the mm/dd/yy portion of the incoming data, convert that to proper MySQL date format and then perform my date functions.
Anyway any help would be greatly appreciated.
That's not very difficult with MySQLs date and time functions. STR_TO_DATE does what you need for the import:
the format I receive the data in is mm/dd/yy hh:mm AM/PM or null.
You get your DATETIME value with
STR_TO_DATE(yourValue, '%m/%d/%y %h:%i %p')
You find the specifiers for STR_TO_DATE at the description of the function DATE_FORMAT
For the export you do the reverse with the already mentioned function DATE_FORMAT with exact the same format string:
SELECT DATE_FORMAT(your_datetime_col, '%m/%d/%y %h:%i %p')
Have a look at this Demo
You can do the conversion at the INSERT statement like that:
INSERT INTO example (date_time) VALUES
(STR_TO_DATE('09/26/14 07:30 AM', '%m/%d/%y %h:%i %p'));
See it working in the updated Demo
Customizing an import with LOAD DATA INFILE
Let's have a table example with two columns id and date_time as
CREATE TABLE example (
id INT NOT NULL PRIMARY KEY,
date_time DATETIME
);
We have further a CSV file example.csv with data like that:
id,date
1,09/26/14 07:30 AM
2,07/23/14 07:30 PM
To import this file with LOAD DATA INFILE, you will use this statement:
LOAD DATA INFILE 'path/on/server/to/example.csv'
INTO TABLE example
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
IGNORE 1 LINES -- because of the column headers in the first line
(id, #var1) -- You've got to map every field of your csv file to a column
-- of your table.
-- You've got to list the names of the columns of your table,
-- not the headers in the csv file.
-- if one field should be ignored, use another variable for this
-- field.
SET date_time = STR_TO_DATE(#var1, '%m/%d/%y %h:%i %p');
If your dates in the csv files contains the literal string 'null' that indicates a NULL value, then use the CASE operator:
date
09/26/14 07:30 AM
null
07/23/14 07:30 PM
then we've got to use
LOAD DATA INFILE 'path/on/server/to/example.csv'
INTO TABLE example
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\r\n' -- your line endings
IGNORE 1 LINES -- because of the column headers in the first line
(#var1) -- read all parts of the date in variables
SET completionTime = CASE
WHEN #var1 = 'null' THEN NULL
ELSE STR_TO_DATE(#var1, '%m/%d/%y %h:%i %p')
END;
The problem with fields containing the separator, in this case the comma, you already solved with enclosing those fields (or simply all) with an enclosing character.
But we really should have a look at your real format.
The real answer to your question:
If you have untechnical people running Sequel Pro imports and need data manipulation, then you need to write and import/export script that users can upload and download an excel sheet from.
However...
I had a similar problem with importing dates with Sequel Pro. So here's a "half" solution. Take from it what you will.
(Note: this question was asked 5 years ago so this is for the benefit of anyone who runs across the question and runs into a similar Sequel Pro problem.)
This is a "half" solution:
Reformat the DateTime inside Excel first.
select column
Go to Format > Cells
Select "custom"
For type use "yyyy-mm-dd hh:mm:ss"
For 'null' values enter an obvious wrong date like "2099-01-01 00:00:00:00".
Import into Sequel Pro.
Open Sequel Pro
Go to View > Show Console (Do this to avoid a Sequel Pro CSV import crash)
Open your database connection and select the table.
Import the CSV.
Note any errors that occur
Clean up your data post-import.
(I told you this was a half solution)
In Sequel Pro run this query: UPDATE your_table SET your_column = NULL WHERE your_column = '2099-01-01 00:00:00:00';

MySql load data infile STR_TO_DATE returning blank?

i'm importing 1m+ records into my table from a csv file.
Works great using the load data local infile method.
However, the dates are all different formats.
A quick google lead me to this function:
STR_TO_DATE
However, when I implement that, I get nothing, an empty insert. here's my SQ cut down to include one date (I've 4 with the same issue) and generic column names:
load data local infile 'myfile.csv' into table `mytable`
fields terminated by '\t'
lines terminated by '\n'
IGNORE 1 LINES
( `column name 1`
, `my second column`
, #temp_date
, `final column`)
SET `Get Date` = STR_TO_DATE(#temp_date, '%c/%e/%Y')
If I do:
SET `Get Date` = #temp_date
The date from the csv is captured in the the format it was in the file.
However when I try the first method, my table column is empty. I've changed the column type to varchar (255) from timestamp to captre whatever is going in, but ultimatly, I want to capture y-m-d H:i:s (Not sure if STR_TO_DATE can do that?)
I'm also unsure as to why I need the # symbol.. google failed me there.
So, my questions are:
Why do I need the # symbol to use this function?
Should the data format ('%c/%e/%Y') be the format of the inputted data or my desired output?
Can I capture time in this way too?
sorry for the large post!
Back to Google for now...
Why do I need the # symbol to use this function?
The # symbol means that you are using a variable, so the read string isnt put right away into the table but into a memory pice that lets you operate with it before inserting it. More info in http://dev.mysql.com/doc/refman/5.0/en/user-variables.html
Should the data format ('%c/%e/%Y') be the format of the inputted data or my desired output?
Its the format of the inputted data, more info in http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_str-to-date
Can I capture time in this way too?
You should be able to as long as you chose the correct format, something like
STR_TO_DATE(#temp_date,'%c/%e/%Y %h:%i:%s');
I had this problem. What solved it for me was making sure I accounted for whitespace that weren't delimiters in my load file. So if ',' is the delimiter:
..., 4/29/2012, ...
might be interpreted as " 4/29/2012"
So should be
...,4/29/2012,...

How to convert csv date format to into mysql db

At csv file, the date field is in such format:
2/9/2010 7:32
3/31/2011 21:20
I am using php + mysql for development.
I need to read it and store into mysql db.
final value to store in mysql should be format as below:
2010-02-09 07:32:00
What's the correct way of it?
Is mysql syntax alone can handle the conversion easily?
Use the STR_TO_DATE() function.
Example
STR_TO_DATE('3/31/2011 21:20', '%c/%e/%Y %H:%i');
I face the same issue and after little research this is how i resolved it-
LOAD DATA LOCAL INFILE 'D:/dataupload.csv' INTO TABLE table1
FIELDS TERMINATED BY ',' ENCLOSED BY '' LINES TERMINATED BY '\r\n' (#d1,col2,col3,col4)
SET col1 = date_format(str_to_date(#d1, **'%m/%d/%Y'**), **'%Y-%m-%d'**)
Details:
'%m/%d/%Y' - this is the format of date in my CSV file
'%Y-%m-%d' - this is the mysql format in which i want to convert my CSV field date while inserting data
col1 - is the actual column of my table (having date data type)
#d1 - is the dummy variable to use in set statement, you can take it any variable
I had the same problem (with DATE) and another solution, is to use the native mysql format YYYYMMDD ie 20120209.
I haven't tried with DATETIME but I guess YYYYMMDDhhmmss will work.