mysql STR_TO_DATE function - mysql

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;

Related

Upload dates in unix timestamp format . Mysql DB

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

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 convert Date on MySQL (variable error)

I have created this table on MySQL :
create table Apolo(
Date date,
Name varchar(50)
);
I have imported an excel file :
LOAD DATA LOCAL INFILE 'C:/Users/File.csv'
INTO TABLE Apolo
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 10 ROWS
(#Date, Name);
set Date=str_to_date(#Date,'%d/%m/%Y');
and I get the error :
Error Code: 1193. Unknown system variable 'Date'
If I do not put this line :
set Date=str_to_date(#Date,'%d/%m/%Y');
I do not get the error but if I try to use :
select count(*) from Apolo where Date='03/09/2015';
it does not work. So the format is not recognisable.
The date to insert into mysql database should be in the format
YYYY-MM-DD
Example: 2015-02-18
So change the date in csv file to the above specified format and try ..
Check your Date column values in database. By default date format in mysql database is Y-m-d
Do you have values in this format.
Take the semi-colon out of this line
(#Date, Name);
The set should be in the same command. Watch out for the fact that you have a field name that is a reserved word - you may need to escape it.

using linux shell script insert dump data to Mysql

i use below script for insert data to sql from textpad.
#!/bin/bash
mysql --utest -ptest test << EOF
LOAD DATA INFILE 'test.txt'
INTO TABLE content_delivery_process
FIELDS TERMINATED BY ',';
EOF
in my test file i have a format like,
cast , date , name , buy
i can insert but i need format like below,
S.NO | date | name | buy | cast
You can specify the columns you want to import:
From the MySQL Manual:
MySQL LOAD DATA INFILE
The following example loads all columns of the persondata table:
LOAD DATA INFILE 'persondata.txt' INTO TABLE persondata;
By default, when no column list is provided at the end of the LOAD
DATA INFILE statement, input lines are expected to contain a field for
each table column.
If you want to load only some of a table's columns, specify a column
list:
LOAD DATA INFILE 'persondata.txt' INTO TABLE persondata (col1,col2,...);
You must also specify a column list if the order of the fields in the
input file differs from the order of the columns in the table.
Otherwise, MySQL cannot tell how to match input fields with table
columns.
You would include "FIELDS TERMINATED BY '|';" at the end to import data delimited with a '|' symbol.
Hope this helps.
create table [YOUR TABLE] ( `S.NO` INT AUTO_INCREMENT, date DATETIME, name VARCHAR(50), buy VARCHAR(50), cast VARCHAR(50));
Load data local infile 'test.txt' ignore into table [YOUR TABLE] fields terminated by ',' lines terminated by '\n'(cast , date , name , buy);

string to timestamp mysql Error #1411

I'm trying to convert timestamps on the fly when importing a csv file into mysql from string to datetime data type. But I am getting a #1411 - Incorrect datetime value: '2007-03-30 16:01:15' for function str_to_date error.
The SQL:
load data infile 'C:/ProgramData/MySQL/MySQL Server 5.5/data/testfile.csv'
into table test
fields terminated by ','
lines terminated by '\n'
(date, col1,col2,col3,col4)
SET
date = str_to_date(date,'%Y.%m.%d %H:%i:%s.%f');
All rows in the .csv are formated like this:
2007.03.30 16:01:15.901,117.53,117.55,35600000,43700000
I've applied
SELECT str_to_date(date,'%Y.%m.%d %H:%i:%s.%f') FROM test
to sample data that was already stored in mysql, it did work.
The target row date is set to DATETIME.
You need to go via a user variable. As the manual says:
The column list can contain either column names or user variables. With user variables, the SET clause enables you to perform transformations on their values before assigning the result to columns.
User variables in the SET clause can be used in several ways. The following example uses the first input column directly for the value of t1.column1, and assigns the second input column to a user variable that is subjected to a division operation before being used for the value of t1.column2:
LOAD DATA INFILE 'file.txt'
INTO TABLE t1
(column1, #var1)
SET column2 = #var1/100;
In your case:
LOAD DATA INFILE 'C:/ProgramData/MySQL/MySQL Server 5.5/data/testfile.csv'
INTO TABLE test
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
(#date, col1, col2, col3, col4)
SET date = STR_TO_DATE(#date, '%Y.%m.%d %H:%i:%s.%f');