1

I want to create separate rows which will contain one word in each cell. These words are currently stored in several cells in one column.

Below is the format that I have

A
play, games, online, free, fun
fun, play, arcade, online
racing, funny, play

To be converted into below

B
play
games
online
free
fun
arcade
racing
funny

Please note, if one row is already created for one word it should not be repeated.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
sam k
  • 21
  • 4

1 Answers1

0

This is something that is typically better done in something other than SQL, like java.

Pseudo code could be:

List<String> names = jdbcTemplate.query("select A from your_table", new RowMapper() {
    public Object mapRow(ResultSet resultSet, int i) throws SQLException {
        return resultSet.getString(1);
    }
});

for (String name : names) {
    String[] strings = name.split("[\\w,]");
    for (int i = 0; i < strings.length; i++) {
        String string = strings[i];
        jdbcTemplate.update("insert ignore into new_table (B) values (?)", string);
    }

}
Andreas Wederbrand
  • 38,065
  • 11
  • 68
  • 78