8

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

Community
  • 1
  • 1
Kajiyama
  • 3,393
  • 8
  • 26
  • 38

3 Answers3

2

You can try this:

select * 
from my_table 
where column_name like ('ABC%') or column_name like ('MOP%');
Robert
  • 25,425
  • 8
  • 67
  • 81
Jarvis Stark
  • 611
  • 5
  • 11
  • As mentioned in question. I am using dynamic set and therefore i cant use neither `OR` nor `UNION`. – Kajiyama Apr 23 '15 at 08:27
0

Would using IN be enough?

i.e.

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

You could also replace the IN clause with a select from another table.

Also you could use a VBA function:

select * from my_table where IsValidColumnName(column_name);

The IsValidColumnName would be a simple function that returns a bool if it matches whatever conditions you want.

To add the function, create a new module and then you can enter something like:

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

Notice the Like statements changed, LIKE in VBA uses the DOS wildcards, you had SQL Server ones in your question.

cjb110
  • 1,310
  • 14
  • 30
-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+'%';