当前位置: 首页 > news >正文

大数据-玩转数据-oracel字符串分割转化为多列

一、建表

create table split_string_test(id integer  primary key,test_string varchar2(500)
);

二、插入测试数据

insert into split_string_test values(1, '10,11,12,13,14,22');
insert into split_string_test values(2, '22,23,24');
insert into split_string_test values(3, '6,7,8,9');

三、语句

WITH  cntr  AS
( SELECT  LEVEL  AS lvlFROM  dualCONNECT BY  LEVEL  <= 1 +  (SELECT  MAX ( length(a.test_string) - length(replace(a.test_string, ',')) + 1) FROM  split_string_test a)                
) 
SELECT  b.id, b.test_string , lvl, REGEXP_SUBSTR( b.test_string, '([^,]+)', 1, lvl)  AS split_str
FROM  split_string_test b , cntr 
where  (lvl  <=  length(b.test_string) - length(replace(b.test_string, ',')) + 1)
and (REGEXP_SUBSTR  ( b.test_string, '([^,]+)', 1, lvl)  IS NOT NULL
OR  b.test_string  IS NULL )
ORDER BY  b.id, lvl;

注:取字符串分拆最大的数

SELECT  MAX ( length(a.test_string) - length(replace(a.test_string, ',')) + 1)  FROM  split_string_test a

或:

SELECT b.id,b.test_string,lvl,REGEXP_SUBSTR(b.test_string, '([^,]+)', 1, lvl) AS split_strFROM split_string_test b,(SELECT LEVEL AS lvlFROM dualCONNECT BY LEVEL <= 1 + (SELECT MAX(length(a.test_string) -length(replace(a.test_string, ',')) + 1)FROM split_string_test a))where (lvl <=length(b.test_string) - length(replace(b.test_string, ',')) + 1)and (REGEXP_SUBSTR(b.test_string, '([^,]+)', 1, lvl) IS NOT NULL ORb.test_string IS NULL)ORDER BY b.id, lvl;

四、结果

在这里插入图片描述

http://www.lryc.cn/news/169607.html

相关文章:

  • GCP设置Proxy来连接Cloud SQL
  • Python:为何成为当下最热门的编程语言?
  • 【echarts入门】:vue项目中应用echarts
  • Seata 源码篇之AT模式启动流程 - 上 - 02
  • FFMPEG视频压缩与Python使用方法
  • SpringMVC自定义注解---[详细介绍]
  • 5.4 转换数据
  • 雷池社区WAF:保护您的网站免受黑客攻击 | 开源日报 0918
  • 链表反转-LeetCode206
  • 北邮22级信通院数电:Verilog-FPGA(3)实验“跑通第一个例程”modelsim仿真及遇到的问题汇总(持续更新中)
  • 4G工业路由器,开启智能工厂,这就是关键所在
  • 计组-机器字长、存储字长、指令字长以及和他们有关的机器位数
  • 解决express服务器接收post请求报错:“req.body==> undefined“
  • 5.zigbee的开发,串口putchar重定向(使用print),单播实验,usb抓包实验
  • 解决AU报“MME无法使用“问题
  • Maven 安装配置
  • vscode 配置网址
  • git 本地工作区和仓库区基本使用
  • 有效的括号(栈的高频面试题)
  • GIS跟踪监管系统电子围栏
  • 蓝桥杯2023年第十四届省赛真题-买瓜--Java题解
  • Chatbot到底提供了哪些便利?来看看“中文版Chatbase”
  • 2023-09-18 LeetCode每日一题(打家劫舍 III)
  • Python一行代码实现文件共享【内网穿透公网访问】
  • uni-app 之 下拉刷新,上拉加载,获取网络列表数据
  • 笔记1.2 计算机网络结构
  • 使用Ansible Template模块进行配置文件管理
  • Secrets of RLHF in Large Language Models Part I: PPO
  • Java手写AVL树应用拓展案例
  • vue3+ts+uniapp小程序封装获取授权hook函数