我想准备一个查询,比如:
SELECT name
FROM Employee
WHERE name LIKE IN ('alex%','raj%','rag%') 这在SQL Server中可能吗?
发布于 2015-12-31 14:21:11
在SQL Server中是可能的吗?
不不可能。
作为问题的解决方案,您需要使用OR like
WHERE name LIKE 'alex%' OR name LIKE 'raj%' OR name LIKE 'rag%'旁注:
你也可以有一个技巧,比如,假设你想使用列的前三个字符进行匹配,那么它就会像这样
WHERE LEFT(name, 3) IN ('ale', 'raj', 'rag')发布于 2015-12-31 14:18:38
您需要使用OR而不是IN运算符来执行此操作
SELECT name FROM Employee
WHERE name LIKE 'alex%' OR name LIKE 'raj%' OR name LIKE 'rag%'发布于 2015-12-31 16:00:27
组合使用OR和LIKE的另一种方法是实现CLR正则表达式函数,因为没有原生T-SQL正则表达式支持。
您可以使用以下函数检查给定的字符串是否与某个值匹配:
/// <summary>
/// Indicates whether the regular expression specified in the Regex constructor finds a match in a specified input string.
/// </summary>
/// <param name="sqlPatern">Regular expression</param>
/// <param name="sqlValue">The string to search for a match.</param>
/// <returns></returns>
[SqlFunction(DataAccess = DataAccessKind.None, IsDeterministic = true)]
public static SqlBoolean IsMatch(SqlString sqlPatern, SqlString sqlValue)
{
if (sqlPatern.IsNull || sqlValue.IsNull)
{
return new SqlBoolean(false);
}
else
{
Regex rgx = new Regex(sqlPatern.Value);
return new SqlBoolean(rgx.IsMatch(sqlValue.Value));
}
}您还可以查看有关如何在SQL Server中部署CLR函数的here说明。
那么你的问题就可以这样解决了:
SELECT name
FROM Employee
WHERE [dbo].[fn_Utils_RegexIsMatch] ('^alex.*|^raj.*^rag.*', name) = 1当然,您现在可以使用regex功能进行过滤:-)
https://stackoverflow.com/questions/34542022
复制相似问题