SQL查找和替换字符串的一部分

时间:2021-03-16 19:22:27

I'm trying to find certain text "catid=18" in a string, where each string is different except for this. I've used this query below before, except it only seems to work if you know the entire string.

我试图在字符串中找到某些文本“catid = 18”,其中每个字符串除此之外都不同。我之前使用过这个查询,除非它知道整个字符串似乎才有效。

update TABLE_NAME
set FIELD_NAME = replace(FIELD_NAME, 'findthis', 'replacewiththis'); 

2 个解决方案

#1


1  

Not sure if that is what you want. But it will return 1 if catid=any_num is found and 0 if not:

不确定这是不是你想要的。但如果找到catid = any_num则返回1,否则返回0:

select 'some_text catid=18 some_text' REGEXP 'catid=[0-9]+'

#2


1  

Maybe you need:

也许你需要:

update TABLE_NAME
set FIELD_NAME = 'goodvalue'
WHERE FIELD_NAME = 'badvalue'; 

#1


1  

Not sure if that is what you want. But it will return 1 if catid=any_num is found and 0 if not:

不确定这是不是你想要的。但如果找到catid = any_num则返回1,否则返回0:

select 'some_text catid=18 some_text' REGEXP 'catid=[0-9]+'

#2


1  

Maybe you need:

也许你需要:

update TABLE_NAME
set FIELD_NAME = 'goodvalue'
WHERE FIELD_NAME = 'badvalue';