1

Please answer me if you know how to insert values into columns added into an existing table. Your answer will be highly appreciated

insert into table a
(name, id)
select col
from table2
;

select col from table2; 

this sql col data is 'john','1' this is impassible ?

5
  • Do you mean col has the value 'john','1', as two strings separated by a comma? Commented Jun 19, 2018 at 5:38
  • table2 col data is 'john', '1' . i want write to the sql right that. only use 'col' Commented Jun 19, 2018 at 5:40
  • You shouldn't store data like that... Comma separated data is a pain in SQL. Commented Jun 19, 2018 at 5:41
  • Is the col data literally 'John','1', with those single quotes? Commented Jun 19, 2018 at 5:41
  • yes has single quotes Commented Jun 19, 2018 at 5:44

1 Answer 1

1

You may try using the base string functions here:

INSERT INTO TABLE a (name, id)
SELECT
    SUBSTR(col, 1, INSTR(col, ',') - 1),
    SUBSTR(col, INSTR(col, ',') + 1)
FROM table2;

If you don't want to insert the insert quotes, then we can slightly modify the above to the following:

INSERT INTO TABLE a (name, id)
SELECT
    SUBSTR(col, 2, INSTR(col, ',') - 2),
    SUBSTR(col, INSTR(col, ',') + 2, LEN(col) - INSTR(col, ',') - 2)
FROM table2;

As has been mentioned in the above comments, storing CSV data in your tables is generally not a good idea. But your question is a good one, because you are trying to solve that problem by moving each value to a new column.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.