简体   繁体   English

sql-检查列是否以特定正则表达式开头

[英]sql - check if column begins with a specific regex

I have the following regex [0-9]\\. 我有以下正则表达式[0-9]\\.

I can get all of these occurrences from a specific column using: 我可以使用以下命令从特定列中获取所有这些事件:

select Col1 from tblTest where Col1 like '%[0-9]\.%'

This will return the column will contain the string: "text text text 55. text text text" 这将返回包含字符串的列:“ text text text 55. text text text”

My aim is to find the "55." 我的目的是找到“ 55”。 and wrap it with a "- -" 并用“--”包裹

So essentially, after i run a query the string will then become: "text text text -55.- text text text" 因此,从本质上讲,在我运行查询后,字符串将变为:“文本文本文本-55.-文本文本文本”

How can i find the regex from that column, and replace it with "text text text -" & RegexMatch & "- text text text"? 我如何从该列中找到正则表达式,并将其替换为“ text text text-”和RegexMatch&“-text text text”?

Maybe just use like and replace: 也许只需使用like并替换:

declare @searchStr varchar(10) = '55.';
select replace(Col1, @searchStr, '-' + @searchStr + '-') 
from tblTest
where Col1 like ('%' + @searchStr + '%');

Keep in mind though, the predicate isn't sargable. 但是请记住,该谓词不可保留。

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

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