Oracle行列转换的一点小总结(5)
字符串转换成多列
其实际上就是一个字符串拆分的问题。
CREATE TABLE t_str_col AS
SELECT ID,c1||’,’||c2||’,’||c3 AS c123
FROM t_col_str;
SELECT * FROM t_str_col;
1)substr + instr
适用范围:8i,9i,10g及以后版本
SELECT id,
c123,
substr(c123, 1, instr(c123 || ‘,’, ‘,’, 1, 1) – 1) c1,
substr(c123,
instr(c123 || ‘,’, ‘,’, 1, 1) + 1,
instr(c123 || ‘,’, ‘,’, 1, 2) – instr(c123 || ‘,’, ‘,’, 1, 1) – 1) c2,
substr(c123,
instr(c123 || ‘,’, ‘,’, 1, 2) + 1,
instr(c123 || ‘,’, ‘,’, 1, 3) – instr(c123 || ‘,’, ‘,’, 1, 2) – 1) c3
FROM t_str_col
ORDER BY 1;
2)regexp_substr
适用范围:10g及以后版本
SELECT id,
c123,
rtrim(regexp_substr(c123 || ‘,’, ‘.*?’ || ‘,’, 1, 1), ‘,’) AS c1,
rtrim(regexp_substr(c123 || ‘,’, ‘.*?’ || ‘,’, 1, 2), ‘,’) AS c2,
rtrim(regexp_substr(c123 || ‘,’, ‘.*?’ || ‘,’, 1, 3), ‘,’) AS c3
FROM t_str_col
ORDER BY 1;
分享到: | |
没有评论