Mysql convert 'CYYMM' to 'YYMM' - mysql

Suck with the following:
$loaddata = "LOAD DATA INFILE 'filename.csv'
INTO TABLE tb1
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\\r\\n'
IGNORE 1 LINES
(
Entity,
HK,
#Period,
)
SET Period = STR_TO_DATE(#Period,'%C%YY%MM')
";
which gives me and sql syntax error near
) SET Period = STR_TO_DATE(#Period,'%C%YY%MM')
Period is a DATE variable. for the period Oct-13 the cvs will show 11310.
tks in advance!

You have a superfluous comma after #Period:
$loaddata = "LOAD DATA INFILE 'filename.csv'
INTO TABLE tb1
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\\r\\n'
IGNORE 1 LINES
(
Entity,
HK,
#Period -- , removed here
)
SET Period = STR_TO_DATE(#Period,'%C%YY%MM')
";
However, your date format string is almost certainly incorrect. %C, %YY and %MM are invalid specifiers. See DATE_FORMAT().

Related

Error importing csv dates into a mysql server

The following code doesn't work on the part of importing dates and i cant figure why. Dates in the csv are like DD/MM/YYYY and the error is it imports all the data but leaves null every date. Also error says:
ER_UNKNOWN_SYSTEM_VARIABLE: Unknown system variable 'FECHA_POSICION'
Lines in the csv file look like:
EDC00001,66600/7089855,21/01/2021,21/01/2021,"DEPOSIT Deposit",4000,4000
EDC00002,66600/7089855,29/01/2021,29/01/2021,CFDs,"-9,94","3990,06"
USE DATA_BASE;
CREATE TABLE ESTADO_DE_CUENTA (
ID_OPERACION VARCHAR(20) NOT NULL PRIMARY KEY,
ID_CUENTA VARCHAR(20),
FECHA_POSICION DATE,
FECHA_VALOR DATE,
CONCEPTO VARCHAR(100),
IMPORTE FLOAT(12, 2),
SALDO_EN_EFECTIVO FLOAT(12, 2)
);
LOAD DATA LOCAL INFILE 'PATH.csv' INTO TABLE ESTADO_DE_CUENTA2
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(ID_OPERACION, ID_CUENTA, #FECHA_POSICION, #FECHA_VALOR, CONCEPTO, IMPORTE,
SALDO_EN_EFECTIVO)
SET FECHA_POSICION = STR_TO_DATE(#FECHA_POSICION, '%d/%m/%Y')
SET FECHA_VALOR = STR_TO_DATE(#FECHA_VALOR, '%d/%m/%Y')
You can call SEt only once and all columsn have to be separated by comma
Like
USE DATA_BASE;
LOAD DATA LOCAL INFILE 'PATH.csv' INTO TABLE ESTADO_DE_CUENTA2
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(ID_OPERACION, ID_CUENTA, #FECHA_POSICION, #FECHA_VALOR, CONCEPTO, IMPORTE,
SALDO_EN_EFECTIVO)
SET `FECHA_POSICION` = STR_TO_DATE(#FECHA_POSICION, '%d/%m/%Y') , `FECHA_VALOR` = STR_TO_DATE(#FECHA_VALOR, '%d/%m/%Y')

LOAD DATA LOCAL INFILE Manipulate Some Date Columns

I have a csv with 3 date columns but each is formatted dd/mm/yyyy H:i:s i.e 27/05/2019 20:25:00
I am trying to manipulate these to insert using LOAD DATA INFILE without any success:
My state looks like this:
LOAD DATA LOCAL INFILE '/file.csv'
INTO TABLE db_table FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
SET promotion_starts = str_to_date(#column7, '%d/%m/%Y %H:%i:%s'),
promotion_ends = str_to_date(#column8, '%d/%m/%Y %H:%i:%s'),
date_added = str_to_date(#column17, '%d/%m/%Y %H:%:%i:%s')
All other data inserts fine but the date columns are all null
You're missing the line of the query that specifies how the fields of the CSV file correspond to table columns, and defines #column7 and #column8.
LOAD DATA LOCAL INFILE '/file.csv'
INTO TABLE db_table FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(foo, bar, baz, xxx, yyy, zzz, #column7, #column8, aaa, bbb, ccc, ddd, eee, fff, ggg, hhh, #column17, iii, jjj)
SET promotion_starts = str_to_date(#column7, '%d/%m/%Y %H:%i:%s'),
promotion_ends = str_to_date(#column8, '%d/%m/%Y %H:%i:%s'),
date_added = str_to_date(#column17, '%d/%m/%Y %H:%:%i:%s')
Replace all the column names I made up with the actual column names in your table that correspond to the CSV fields.

MySQL: LOAD DATA LOCAL INFILE adds extra character '\r'

I have a table like this:
CREATE TABLE `tblinquiries` (
`UID` varchar(50) DEFAULT NULL,
`ReviewDate` date NOT NULL,
`InquiryId` varchar(50) DEFAULT NULL,
`AuditStatus` varchar(50) DEFAULT NULL,
PRIMARY KEY (`InquiryId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
I have a csv file with data:
UID,ReviewDate,InquiryId,AuditStatus
UID1,2018-07-06,109814969,Check
UID2,2018-07-06,109866072,Check
UID3,2018-07-06,109911408,Check
UID4,2018-07-06,109798278,Check
I use below command to to upload the data:
$location = '../uploads/';
$name = $_FILES["file"]["name"];
$filePath = $location.$name;
$table = 'tblinquiries';
LOAD DATA LOCAL INFILE "'.$filePath.'"
INTO TABLE '.$table.'
FIELDS TERMINATED by \',\' OPTIONALLY ENCLOSED BY \'"\'
LINES TERMINATED BY \'\n\'
IGNORE 1 LINES
It uploads the data but adds extra character "\r" from the second line. I exported the data and got like below:
('UID4', '2018-07-06', '109798278', 'Check'),
('UID1', '2018-07-06', '109814969', 'Check\r'),
('UID2', '2018-07-06', '109866072', 'Check\r'),
('UID3', '2018-07-06', '109911408', 'Check\r');
After running:
SELECT AuditStatus, LENGTH(AuditStatus) FROM `tblinquiries`
got:
AuditStatus LENGTH(AuditStatus)
Check 5
Check 6
Check 6
Check 6
How can I solve this?
I would assume that your source data has those \r control characters, because LOAD DATA doesn't typically add data to the source file (unless you tell it to do so, which does not appear to be the case). We can try running RTRIM on the AuditStatus column:
LOAD DATA LOCAL INFILE "'.$filePath.'"
INTO TABLE '.$table.'
FIELDS TERMINATED by \',\' OPTIONALLY ENCLOSED BY \'"\'
LINES TERMINATED BY \'\n\'
IGNORE 1 LINES
(UID, ReviewDate, InquiryId, #AuditStatus)
SET AuditStatus = RTRIM(#AuditStatus);
As #Sloan suggested I changed the line terminators and that solved the problem.
Here is the final code.
LOAD DATA LOCAL INFILE "'.$filePath.'"
INTO TABLE '.$table.'
FIELDS TERMINATED by \',\' OPTIONALLY ENCLOSED BY \'"\'
LINES TERMINATED BY \'\r\n\'
IGNORE 1 LINES

MySQL Select INTO csv file separator working incorrectly

I am trying to export some data to csv but facing some issue with the "," delemeter.
I am running the below query:
SELECT c.NAME
FROM company c
WHERE COMPANY_ID = 1
INTO OUTFILE 'E:\\ab.csv' FIELDS OPTIONALLY ENCLOSED BY '' TERMINATED BY ',' ESCAPED BY '' LINES TERMINATED BY '\n';
If my name does not contain any "," then the csv is correctly populated but if my "name" field contains "," then data is split into two rows.
For ex:
c.Name = "Google INC" -> Works fine
c.Name = "Google,INC" -> it creates csv with two different column "Google" and "INC".
Kindly suggest what should I do.
Based on the link at the comment you can try something like this
SELECT c.NAME
FROM company c
WHERE COMPANY_ID = 1
INTO OUTFILE 'E:\\ab.csv' FIELDS OPTIONALLY ENCLOSED BY '' TERMINATED BY '",' ESCAPED BY '' LINES TERMINATED BY '\n';
Marking ", instead ,

MySQL LOAD DATA INFILE with comma as decimal separator

How can I import a TSV file when the numbers use comma as a decimal separator?
LOAD DATA INFILE '$filename' INTO TABLE dados_meteo IGNORE 3 LINES
($fields[0], $fields[1], $fields[2], $fields[3], $fields[4], $fields[5])
SET POM='$pom'
;
Try to replace ',' to '.' when loading.
For example -
LOAD DATA INFILE 'file.csv' INTO TABLE dados_meteo
(#var1, #var2)
SET column1 = REPLACE(#var1, ',', '.'), column2 = REPLACE(#var2, ',', '.')
If $field[0] is your numeric:
LOAD DATA INFILE '$filename' INTO TABLE dados_meteo IGNORE 3 LINES
(#var1, $fields[1], $fields[2], $fields[3], $fields[4], $fields[5])
SET POM='$pom', $field[0] = CONVERT(REPLACE(#var1,',', ''), DECIMAL(10));
If more are numeric, simply repeat the pattern with a #var2, #var3, etc. You'll want to replace the DECIMAL(10) with whatever your field really is.