How can i introduce multiple conditions in LIKE operator for MS Access SQL

时间:2015-04-23 05:19:44

标签: sql ms-access-2010

I would like to know, if there is any way, how to achieve something like this in access sql.

select * from my_table where column_name like ('ABC%', 'MOP%');

I tried use this: https://stackoverflow.com/a/1387797/1784053 but this seems not to work in Access.

Is there way on how to achieve anything like multiple like conditioning based on dynamic set of conditions? This means, that I cant use OR neither UNION because my set of conditions is dynamic.

Similar question: How can i introduce multiple conditions in LIKE operator

3 个答案:

答案 0 :(得分:2)

你可以试试这个:

select * 
from my_table 
where column_name like ('ABC%') or column_name like ('MOP%');

答案 1 :(得分:0)

使用IN是否足够?

select * from my_table where column_name in ("ABC", "MOP");

您也可以使用另一个表中的select替换IN子句。

您也可以使用VBA功能:

select * from my_table where IsValidColumnName(column_name);

IsValidColumnName是一个简单的函数,如果它匹配你想要的任何条件,它将返回一个bool。

要添加该功能,请创建一个新模块,然后输入如下内容:

Public Function IsValidColumnName(columnName As String) As Boolean

If columnName Like "ABC*" Or columnName Like "MOP*" Then
    IsValidColumnName = True
Else
    IsValidColumnName = False
End If

End Function

注意Like语句已更改,VBA中的LIKE使用DOS通配符,您的问题中有SQL Server。

答案 2 :(得分:-1)

Try this

Function

CREATE  FUNCTION [dbo].[fn_Split](@text varchar(8000), @delimiter varchar(20))
RETURNS @Strings TABLE
(   
  position int IDENTITY PRIMARY KEY,
  value varchar(8000)  
)
AS
BEGIN

DECLARE @index int
SET @index = -1

WHILE (LEN(@text) > 0)
  BEGIN 
    SET @index = CHARINDEX(@delimiter , @text) 
    IF (@index = 0) AND (LEN(@text) > 0) 
      BEGIN  
        INSERT INTO @Strings VALUES (@text)
          BREAK 
      END 
    IF (@index > 1) 
      BEGIN  
        INSERT INTO @Strings VALUES (LEFT(@text, @index - 1))  
        SET @text = RIGHT(@text, (LEN(@text) - @index)) 
      END 
    ELSE
      SET @text = RIGHT(@text, (LEN(@text) - @index))
    END
  RETURN
END

Query

select * from my_table inner join (select value from fn_split('ABC,MOP',','))
as split_table on my_table.column_name like '%'+split_table.value+'%';