admin

用逗号将一个字符串分割成几列

sql

例如,我有下表:

| Block                       |
| abcdefgh,12kjkjkj,231wewoxyz|

如何将其转换为:

| Block1   | Block2   | Block3     |
| abcdefgh | 12kjkjkj | 231wewoxyz |

注意:-每个“块”最多具有8个逗号(因此可以分成9个较小的块)。逗号的数量各不相同;有的有8个逗号,有的有4个,有的有7个,依此类推。每个较小的块将占据一列。

我使用Presto,顺便说一句。

谢谢!


阅读 164

收藏
2021-06-07

共1个答案

admin

在我看来,您只需要使用split_partfunction创建列,就像这样:

SELECT
    split_part(Block, ',', 1) AS Block1,
    split_part(Block, ',', 2) AS Block2,
    split_part(Block, ',', 3) AS Block3,
    split_part(Block, ',', 4) AS Block4,
    split_part(Block, ',', 5) AS Block5,
    split_part(Block, ',', 6) AS Block6,
    split_part(Block, ',', 7) AS Block7,
    split_part(Block, ',', 8) AS Block8,
    split_part(Block, ',', 9) AS Block9
FROM
    your_table
2021-06-07