简体   繁体   English

SQL语句使用模式删除字符串的一部分

[英]SQL statement to remove part of a string using a pattern

I have a table column with semicolon delimited values illustrated as follows: 我有一个用分号分隔的值的表列,如下所示:

table1.ID | table1.column1 
-----------------------------------------------------
   1      | test1=aaa;test2=bbb;test3=ccc;
   2      | test1=akkaa;test2=bkjkjb;test3=cckjklc;
   3      | test1=ajaa;test2=bjhjbb;test3=sss;

I need to remove all test2=xxxx where xxxx can be of any value. 我需要删除所有test2=xxxx ,其中xxxx可以是任何值。

I started the following query which I need to complete; 我开始以下查询,我需要完成该查询; how I can specify a given pattern / expression? 如何指定给定的模式/表达式?

UPDATE table1
SET column1 = Replace(column1, 'test2=', '')
WHERE column1  Is Not Null;

Thanks in advance 提前致谢

For MSSQL: 对于MSSQL:

Try this query: (I didn't used any pattern[I don't know the possibility]. What I did is replace the string content of test2=something; with empty string. The CharIndex function is used to locate the Test2 and test3 location.) 尝试以下查询:(我没有使用任何模式[我不知道可能性]。我所做的是用空字符串替换test2 = something;的字符串内容。CharIndex函数用于查找Test2和test3位置。)

 UPDATE table1 SET Column1  = 
      REPLACE(Column1, substring(Column1, CharIndex('test2=', Column1), 
           (CharIndex( 'test3=', Column1) - CharIndex('test2=', Column1))), '')

--Live Demo Here -现场演示

for oracle: 对于oracle:

update table1
set column1=substr(table1.column1,1,instr(table1.column1,';',1,1)-1)||substr(table1.column1,instr(table1.column1,';',1,2));
commit;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM