SQL Server T-SQL中的REGEXP_LIKE转换

Zak*_*ias 4 sql sql-server oracle design-patterns patindex

我在一份需要转换为SQL Server的旧报告中遇到过这一行.

REGEXP_LIKE (examCodes, learner_code)
Run Code Online (Sandbox Code Playgroud)

examCodes是源,mathner_code是模式.我知道SQL Server没有REGEXP_LIKE,并且大多数地方都告诉您使用PATINDEX.

这是我认为这将工作:

PATINDEX(learner_code, examCodes)
Run Code Online (Sandbox Code Playgroud)

但我得到错误:

Msg 4145, Level 15, State 1, Line 54
An expression of non-boolean type specified in a context where a condition is expected, near 'WHERE'.
Run Code Online (Sandbox Code Playgroud)

在MSDN上,语法指定为,

PATINDEX ('%pattern%',expression) 
Run Code Online (Sandbox Code Playgroud)

但是learner_code是一个字段,我不能指定一个模式?

我首先没有写这份报告所以我很困惑它无论如何都在寻找什么样的模式.

非常感谢

Mud*_*san 6

WHERE PATINDEX ('%pattern%',expression)  !=0
Run Code Online (Sandbox Code Playgroud)

如果找到pattern,则PATINDEX返回非零值,您需要在WHERE子句中进行比较.一个WHERE子句必须后跟返回true/false的比较操作.

可能是您正在使用PATINDEX而不进行比较,这就是错误消息在WHERE子句附近显示非布尔表达式的原因.

使用通配符搜索模式learner_code

WHERE PATINDEX ('%' + CAST(learner_code AS VARCHAR) +'%',examCodes)  !=0
Run Code Online (Sandbox Code Playgroud)