Sql 通过检查条件将一列拆分为多列
我有一张表格,格式如下:Sql 通过检查条件将一列拆分为多列,sql,oracle,oracle-sqldeveloper,Sql,Oracle,Oracle Sqldeveloper,我有一张表格,格式如下: ID | Marks | Maths | Science | Grade 1 | 72_93_A | | | A 2 | 43_B | | | B 现在,我需要拆分带有分隔符的Marks列字符串,并在mathematics和Science列中插入值。另外,如果Marks列数据有三个标记(72_93_A),那么我需要同时更新mathematic列和Science列,否则我只需要更新mathematic
ID | Marks | Maths | Science | Grade
1 | 72_93_A | | | A
2 | 43_B | | | B
现在,我需要拆分带有分隔符的Marks列字符串,并在mathematics和Science列中插入值。另外,如果Marks列数据有三个标记(72_93_A),那么我需要同时更新mathematic列和Science列,否则我只需要更新mathematic列。我需要如下所述的输出
ID | Marks | Maths | Science | Grade
1 | 72_93_A | 72 | 93 | A
2 | 43_B | 43 | | B
是否有人可以在此条件更新场景中帮助我。您可以使用执行更新:
select
regexp_substr(Marks, '([^_]+)(_[^_]+){2}$', 1, 1, null, 1) as Maths,
regexp_substr(Marks, '([^_]+)(_[^_]+){1}$', 1, 1, null, 1) as Science,
regexp_substr(Marks, '([^_]+)(_[^_]+){0}$', 1, 1, null, 1) as Grade
from . . .
update your_table
set Maths = regexp_substr(marks, '\d+', 1, 1),
Science = regexp_substr(marks, '\d+', 1, 2)
where Maths is null
or science is null;
\d+
匹配一个或多个数字,第四个参数是匹配项。您在哪个数据库中?4个人都需要这个吗?另外,您试图解决自己的问题,还是在等待我们为您解决问题?我在Oracle db上。我尝试了子字符串_索引获取,但更新不正确。请编辑问题并修改标签和标题。如果希望在Oracle中使用SQL Server,请不要说“在SQL Server中”。