Upload dates in unix timestamp format . Mysql DB - mysql

I have data with date format 1577234966837.
I uploaded this data in table via command :
load data infile 'C:/file.tsv'
into table table_1
fields terminated by '\t'
lines terminated by'\n'
ignore 1 lines (value, #timestamp)
set timestamp = FROM_UNIXTIME(#timestamp);
Command successful, but value in column timestamp is null. Ho to upload this format?

Your code looks right, but have problem with data type convertation.
In MySQL unixtime is number of second from 1970-01-01 00:00:00.
In your case number looks as JavaScript time in milliseconds, so for right convertion you should to divide the number by 1000
select from_unixtime(1577234966837); -- result is NULL
select from_unixtime(1577234966837/1000); -- result 2019-12-25 00:49:26.8370
DB fiddle link
So right import command should be like:
load data infile 'C:/file.tsv'
into table table_1
fields terminated by '\t'
lines terminated by'\n'
ignore 1 lines (value, #timestamp)
set timestamp = FROM_UNIXTIME(#timestamp/1000);

Related

Import datetime field correctly into mysql table using load data infile

I'm facing problem for datetime fields while importing file using mysql command load data local infile, but if I import the same file using Table Data Import Wizard in MySQL Workbench it works perfectly. But this option is extremely slow. It's been more than 2 hrs only 10% data imported for only 12k data sample. As I found out that load data local infile command is much faster, I'm going for it but stuck currently for datetime field.
Sample data file content: (Last 2 fields are created_at and updated_at fields which are having problem while importing)
740c02a0-54d9-11eb-b1aa-1312597791fc,RAMKKUMAR,Test#GMAIL.COM,2018-16-05 21:05:00,2018-22-11 4:47:00
Sample Code:
load data local infile 'C:/Newwamp64/bin/mysql/mysql5.7.26/bin/for.csv'
into table mytable.users
FIELDS OPTIONALLY ENCLOSED BY '"'
TERMINATED BY ','
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES
SET created_at = STR_TO_DATE(#created_at, '%Y-%d-%m %H:%i:%s');
Result
All fileds are fine, except following two -
created_at -> NULL
updated_at -> 0000-00-00 00:00:00

mysql STR_TO_DATE function

I would like to convert MySQL string to required date format .
I have below lines in a file.
30-06-2017,clarke
31-07-2018,warner
my table is having 2 columns .
Column1 datatype :: date
Column2 datatype :: varchar(30)
I have executed below query
load data local infile 'test.txt' into table sample fields terminated by ',' set column1=str_to_date(#c1,'%d-%m-%Y') ;
Column1 data was not loaded and I got below warnings.
Data wad truncated for column1 at row1
May I know what is wrong in the sql query which I am using ?
You have to include the columns (#c1, #c2) from the file. Following command works fine.
LOAD DATA LOCAL INFILE 'test.txt' INTO TABLE sample FIELDS TERMINATED BY ',' (#c1, #c2) SET column1=STR_TO_DATE(#c1,'%d-%m-%Y'), column2=#c2;

Mysql read string as datetime

Hello I'm using LOAD DATA INFILE to populate a table in MySQL.
LOAD DATA INFILE 'test.txt'
INTO TABLE myTestTable
FIELDS TERMINATED BY '\t'
IGNORE 1 LINES;
Everything is working peachy except that there is a datetime column in my data that is formatted without any delimiter between the date and time sections. Like so:
SomeDateColumn
20050101081946
When I read this in, MySQL replaces all the dates with dummy values. Is there a way to have MySQL read this in correctly straight from a file?
Thanks!
You may call STR_TO_DATE when you run LOAD DATA, and convert the text date to a bona fide date on the fly:
LOAD DATA INFILE 'test.txt'
INTO TABLE myTestTable
FIELDS TERMINATED BY '\t'
IGNORE 1 LINES
(
col1, col2, #var1 -- list out all columns here
)
SET SomeDateColumn = STR_TO_DATE(#var1, '%Y%m%d%h%i%s');

How to use UTC_TIMESTAMP() in csv file

I need a way to use the UTC_TIMESTAMP() function in a CSV file.
Currently I'm using the following Syntax;
LOAD DATA INFILE '/path/to/file.csv'
INTO TABLE my_table FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n';
I'm not quite sure what to do to keep the UTC_TIMESTAMP() from being enclosed in quotes. When entered in the database, I get the following result
"0000-00-00 00:00:00"
This was the only example I could find on stack overflow or on Google for converting a string to a MYSQL value.
MySQL CSV import: datetime value
I solved the problem by following the MySQL documentation on this page.
https://dev.mysql.com/doc/refman/5.7/en/load-data.html
About halfway down the page there is a section that shows you how to create variables for rows and then set table rows equal to native mysql functions or values assigned to those variables(Whichever you choose).
The example in the documentation that I'm referring to looks like this.
LOAD DATA INFILE 'file.txt'
INTO TABLE t1
(column1, column2)
SET column3 = CURRENT_TIMESTAMP;
I fixed my problem by restructuring my code like this...
LOAD DATA LOCAL INFILE '/path/to/file.csv'
INTO TABLE veh_icodes FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
(id, vcode, year, make, model, body_style, tran_type, engine_cap, drive_train, doors, trim, created_by, updated_by, #created_at, #updated_at, active)
SET created_at = CURRENT_TIMESTAMP, updated_at = CURRENT_TIMESTAMP;"
I hope this helps someone. :)

Load Data Infile Error with date

I am trying to load a .csv file that has 5 columns into a table that has the same corresponding columns plus a PK. Dates are data type DATE and all others are Varchar(), except PK.
Here is my load data Import:
LOAD DATA INFILE 'C:\Events_Upload.csv'
INTO TABLE db.events
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
Ignore 1 lines
(
Event_Name,
Event_Handle,
Create_Date,
Retire_Date,
Event_Desc
)
The error is :
Error Code: 1292. Incorrect date value: '' for column 'Retire_Date' at row 1
Row one in CSV looks like:
Time to Send Survey,SurvESend,2013-04-10,,Time for the system to send out the Esurveys
How can I make this field NULL and not blank for uploading from CSV?
There is nothing as null in CSV file. What you can do is may be provide some default value for the missing column and replace it with null by the query after insertion into the table.
Or you can have a look at skipping-empty-csv-objects
check this Export null to .csv and https://superuser.com/questions/390031/how-to-write-null-into-csv-from-excel-for-blank-fields