oracle SQL Server T-SQL 中的 REGEXP_LIKE 转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16315658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 01:37:44  来源:igfitidea点击:

REGEXP_LIKE conversion in SQL Server T-SQL

sqlsql-serveroracledesign-patternspatindex

提问by Zakerias

I have come across this line in an old report that needs converting to SQL Server.

我在需要转换为 SQL Server 的旧报告中遇到了这一行。

REGEXP_LIKE (examCodes, learner_code)

examCodes being the source and learner_code being the pattern. I know that SQL Server doesn't have REGEXP_LIKE and most places tell you to use PATINDEX.

ExamCodes 是源代码,learner_code 是模式。我知道 SQL Server 没有 REGEXP_LIKE 并且大多数地方都告诉您使用 PATINDEX。

Here's me thinking that this would work:

我认为这会起作用:

PATINDEX(learner_code, examCodes)

But I get the error:

但我收到错误:

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'.

On MSDN the syntax is specified as,

在 MSDN 上,语法被指定为,

PATINDEX ('%pattern%',expression) 

But learner_code is a field and I can't specify a pattern?

但是 learner_code 是一个字段,我无法指定模式?

I did not write this report in the first place so I'm puzzled to what the pattern it's looking for anyway.

这份报告一开始并不是我写的,所以我很困惑它到底在寻找什么模式。

Many thanks

非常感谢

回答by Mudassir Hasan

WHERE PATINDEX ('%pattern%',expression)  !=0

If pattern is found , PATINDEX returns non zero value and you need to do a comparison in WHERE clause. A WHEREclause must be followed by comparison operation that returns true / false.

May be you are using PATINDEX without doing the comparison and that is why error message shows non boolean expression near WHERE clause.

如果找到模式,PATINDEX 返回非零值,您需要在 WHERE 子句中进行比较。一个WHERE子句必须跟比较的操作,返回真/假。

可能是您在没有进行比较的情况下使用 PATINDEX,这就是错误消息在 WHERE 子句附近显示非布尔表达式的原因。

To search for pattern learner_codewith wildcard character

使用通配符搜索模式learner_code

WHERE PATINDEX ('%' + CAST(learner_code AS VARCHAR) +'%',examCodes)  !=0

回答by Ken Thompson

ORACLE'S REGEXP_LIKE supports actual regular expressions, PATINDEX only supports the % and _ wildcards, the [] list/range, and the ^ list/range negation.

ORACLE 的 REGEXP_LIKE 支持实际的正则表达式,PATINDEX 仅支持 % 和 _ 通配符、[] 列表/范围和 ^ 列表/范围否定。