简体   繁体   English

将csv导入mysql时忽略列的前两个字符

[英]ignore first two characters on a column while importing csv to mysql

I am trying to import a csv file to mysql table, But I need to remove First two characters on particular column before importing to mysql. 我正在尝试将一个csv文件导入到mysql表中,但是我需要在导入到mysql之前删除特定列上的前两个字符。 This is my statment : 这是我的陈述:

 string strLoadData = "LOAD DATA LOCAL  INFILE 'E:/park/Export.csv' INTO TABLE tickets  FIELDS  terminated by ',' ENCLOSED BY '\"'  lines terminated by '\n' IGNORE 1 LINES (SiteId,DateTime,Serial,DeviceId,AgentAID,VehicleRegistration,CarPark,SpaceNumber,GpsAddress,VehicleType,VehicleMake,VehicleModel,VehicleColour,IssueReasonCode,IssueReason,NoticeLocation,Points,Notes)";

Column IssueReasoncode' has data like 'LU12' , But i need to remove the first 2 characters it should have only integers on it and not alpha numeric . IssueReasoncode' has data like 'LU12' ,但是我需要删除前2个字符,它应该只包含整数而不是alpha数字。 I need to remove 'LU' from that column. 我需要从该列中删除'LU'

Is it possible to write like this on left(IssueReasonCode +' '2). 是否可以在left(IssueReasonCode +' '2).这样写left(IssueReasonCode +' '2). This column is varchar(45) and cant be changed now because of large data on it. 该列为varchar(45),由于其上的数据量较大,现在无法更改。

Thanks 谢谢

LOAD DATA INFILE has the ability to perform a function on the data for each column as you read it in (qv here ). LOAD DATA INFILE能够在您读入每列的数据时对其执行功能( 在此处 qv)。 In your case, if you wanted to remove the first two characters from the IssueReasonCode column, you could use: 对于您的情况,如果要从IssueReasonCode列中删除前两个字符,可以使用:

RIGHT(IssueReasonCode, CHAR_LENGTH(IssueReasonCode) - 2)

to remove the first two characters. 删除前两个字符。 You specify such column mappings at the end of the LOAD DATA statement using SET . 您可以使用SETLOAD DATA语句的末尾指定此类列映射。 Your statement should look something like the following: 您的声明应类似于以下内容:

LOAD DATA LOCAL INFILE 'E:/park/Export.csv' INTO TABLE tickets
FIELDS terminated by ','
ENCLOSED BY '\"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(SiteId, DateTime, Serial, DeviceId, AgentAID, VehicleRegistration, CarPark, SpaceNumber,
GpsAddress, VehicleType, VehicleMake, VehicleModel, VehicleColour, IssueReasonCode,
IssueReason, NoticeLocation, Points, Notes)
SET IssueReasonCode = RIGHT(IssueReasonCode, CHAR_LENGTH(IssueReasonCode) - 2)

Referencing this and quoting this example , you can try the below to see if it works 引用和引用这个例子中,你可以试试下面来看看它是否工作

User variables in the SET clause can be used in several ways. SET子句中的用户变量可以以多种方式使用。 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: 以下示例将第一个输入列直接用于t1.column1的值,并将第二个输入列分配给用户变量,该用户变量在用于t1.column2的值之前要进行除法运算:

LOAD DATA INFILE 'file.txt' INTO TABLE t1 (column1, @var1) SET column2 = @var1/100; LOAD DATA INFILE'file.txt'到表t1(column1,@ var1)SET column2 = @ var1 / 100;

string strLoadData = "LOAD DATA LOCAL  INFILE 'E:/park/Export.csv' INTO TABLE tickets  FIELDS  terminated by ',' ENCLOSED BY '\"'  lines terminated by '\n' IGNORE 1 LINES (SiteId,DateTime,Serial,DeviceId,AgentAID,VehicleRegistration,CarPark,SpaceNumber,GpsAddress,VehicleType,VehicleMake,VehicleModel,VehicleColour,@IRC,IssueReason,NoticeLocation,Points,Notes) SET IssueReasonCode = substr(@IRC,2) ;";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM