本文介绍了SQL Server T-SQL中的REGEXP_LIKE转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在需要转换为SQL Server的旧报表中遇到这一行。

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

REGEXP_LIKE (examCodes, learner_code)

考试代码是作为模式的源和学习者代码。我知道SQL Server没有REGEXP_LIKE,大多数地方告诉你使用PATINDEX。

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.

这是我认为这将工作:

PATINDEX(learner_code, examCodes)

但是我收到错误:

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

在MSDN上,语法被指定为

On MSDN the syntax is specified as,

PATINDEX ('%pattern%',expression)

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

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

我没有写这个报告在第一位,所以我很困惑的是什么样的样子无论如何,

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

非常感谢

推荐答案

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

发现,PATINDEX返回非零值,您需要在WHERE子句中进行比较。
A WHERE 子句必须跟随返回true / false的比较操作。


可能您正在使用PATINDEX而不做比较,这就是为什么错误消息在WHERE子句附近显示非布尔表达式。

If pattern is found , PATINDEX returns non zero value and you need to do a comparison in WHERE clause.A WHERE clause 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.

使用通配符

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

这篇关于SQL Server T-SQL中的REGEXP_LIKE转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 05:38