load data infile, dealing with fields with comma
Asked Answered
F

6

6

How do we deal with field with comma when using load data infile? i have this query:

$sql = "LOAD DATA LOCAL INFILE '{$file}' INTO TABLE sales_per_pgs 
        FIELDS TERMINATED BY ','
        LINES TERMINATED BY '\n'
        IGNORE 1 LINES
        (@user_id, @account_code, @pg_code, @sales_value)
        SET
        user_id = @user_id, 
        account_code = @account_code,
        product_group_code = @pg_code,
        sales_value = REPLACE(@sales_value, ',', ''),
        company_id = {$company_id},
        year = {$year},
        month = {$month}";

and a line from the csv looks like this:

139, pg89898, op89890, 1,000,000.00

where 1,000,000.00 is a sales value.

Currently, what is inserted in my database is only "1.

EDIT

The user downloads a form with columns like:

user id, account id, pg id, sales value

where the first three columns user id, account id, pg id, were populated and the sales value column is blank because the user has to fill it up manually... the user uses MS excel to do that...

after the form is completed, he will now upload it, in which i am using the load data infile command...

Fillbert answered 1/8, 2012 at 12:27 Comment(0)
W
11

Your content should really look like:

"139", "pg89898", "op89890", "1,000,000.00"

Then you could add the following to the command:

ENCLOSED BY '"' ESCAPED BY "\\"

And you won't have an issue.

Also, somethign you could try if you don't have any paragraphs or strings with , in them:

FIELDS TERMINATED BY ', '
Wolbrom answered 1/8, 2012 at 12:30 Comment(0)
B
1

You will have to alter the CSV file that is being input or alter the output that generates the CSV file - sounds the same but it isn't.

You can modify the data coming in by encapsulating fields with quotes and update your command so that it recognizes that fields are encapsulated with them using a command like ENCLOSED BY '"'

or

alter your output so that it formats the number as 1000000 rather than 1,000,000

Baucom answered 1/8, 2012 at 12:30 Comment(0)
V
1

had the same problem and used just ENCLOSED BY '"' which fixed my issue since i had mixed numbers and strings which is exctyly what ENCLOSED BY is for , from the manuall :

If you specify OPTIONALLY, the ENCLOSED BY character is used only to enclose values from columns that have a string data type (such as CHAR, BINARY, TEXT, or ENUM):

Valerievalerio answered 1/1, 2020 at 6:8 Comment(1)
Can you please show full query LOAD DATA LOCAL INFILE '/var/www/html/assignor.csv' INTO TABLE assignment FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n' IGNORE 1 LINES (rf_id,or_name,exec_dt,ack_dt); it is not workingFurfural
G
0

In a CSV, comas separate "columns". Since your last value is 1,000,000.00 it is regarded as 3 different columns instead one just one (as intended).

You can either quote each value(column) or change the number format, by removing the commas (,).

Gullet answered 1/8, 2012 at 12:30 Comment(0)
A
0

if your entire file is exactly as you wrote, then maybe you could use fields terminated by ', ' (comma + space), if and only if you don't have that string within any individual value. If you are using Linux (or any other Unix like system) and your field separator is comma + space, you can use sed to replace this separator with something else:

sed 's/, /|/g' myfile.csv > myfile.txt

However, I would recommend what has already been said: modify your input file enclosing each value with quotes or double quotes and use fields terminated by ',' optionally enclosed by '"'.

Remember that your field termination character must be unique, and must not be contained within any individual value.

Agronomy answered 1/8, 2012 at 12:38 Comment(0)
E
0

As a workaround, try this one -

LOAD DATA INFILE
...
FIELDS TERMINATED BY ', '
...
Easternmost answered 1/8, 2012 at 12:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.