1

I uploaded a csv file to MySQL DB but for some reason data from two columns got glued to one.

e.g. the data 0880174V,D should have gone to two different columns.

Is there a way (via sql) i can split the data from this column (i.e. as 0880174V and D) and update the table, without having to delete the record and re-upload? I have a million records.

==CURRENT

Column A       Column B
0880174V,D     

== EXPECTED

Column A       Column B
0880174V        D   

3 Answers 3

6
UPDATE my_table
SET ColumnB = SUBSTRING(ColumnA, INSTR(ColumnA, ',') + 1),
    ColumnA = SUBSTRING(ColumnA, 1, INSTR(ColumnA, ',') - 1)

Do a SELECT TOP first to ease your mind.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much. Although it kind of did the reverse (D 0880174V) instead of (0880174V D), i got the insight. Appreciate your help.
Yap, you are right. Sorry, my bad. Changed the answer in any case. Is late in my time zone, if that is to any aval :)
1

You have to specify field terminator

load data local infile 'file.csv' into myTable
fields terminated by ','
enclosed by '"'
lines terminated by '\n'
(col1,col2)

EDIT: nvm

Comments

1

a simpler solution is to use SUBSTRING_INDEX instead of SUBSTRING with INSTR

UPDATE my_table
SET ColumnB = SUBSTRING_INDEX(ColumnA, ',' , 1),
ColumnA = SUBSTRING_INDEX(ColumnA, ',' , - 1)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.