Skip to content Skip to sidebar Skip to footer

Insert String's Each Values Into A Single Column

Here I want to insert the complete string each values into a column. For which I have written the following script: Example: Table: test create table test ( cola varchar(10), colb

Solution 1:

SQL does not have any mechanism for automatically splitting a string of comma-separated values (your @colb-variable into multiple inserts. You will need to write some code to do this splitting yourself.

Basically, you should do something like this:

  1. In a while loop, use CHARINDEX to determine the position of the next , in @colb. Store this position in a variable.
  2. Use SUBSTRING to retrieve only the characters from @colb up to the position stored in (1).
  3. Call INSERT with @cola, @colc and the value you extracted in (2).
  4. Repeat until no more , found in @colc.

Post a Comment for "Insert String's Each Values Into A Single Column"