MS Access变量字符串搜索

时间:2016-07-29 23:53:10

标签: sql ms-access

我想创建一个查询,用于标识长文本字符串不包含任何子字符串列表的条目。

我拥有的是......

SELECT [other necessary data], [Long text string]
WHERE [Long String] NOT LIKE "*[substring1]*" 
  AND [Long string] NOT LIKE "*[substring2]*" 
  AND [Long string] NOT LIKE "*[substring3]*"

这很好用。然而!我希望排除的子串列表是可变的,例如项目1排除子串1和3,项目2排除子串2,3和17等。

关于如何做到这一点的任何建议?

1 个答案:

答案 0 :(得分:0)

也许这样的事情?然后你只需要调用它,最多可以排除四个字符串,并将sql用于你想做的任何事情。

Function strsql(ex1 as string,Optional ex2 as string = "", _  
Optional ex3 as string = "", Optional ex4 as string = "") as string
strsql = "SELECT [other necessary data], [Long text string] _  
WHERE [Long String] NOT LIKE " & chr(34) & "*" & ex1 & "*" & _  
chr(34)
If ex2 <> "" then  
strsql = strsql & " AND [Long String] NOT LIKE " & chr(34) & _  
"*" & ex2 & "*" & chr(34)  
End if  
If ex3 <> "" then  
strsql = strsql & " AND [Long String] NOT LIKE " & chr(34) & _  
"*" & ex3 & "*" & chr(34)  
End if  
If ex4 <> "" then  
strsql = strsql & " AND [Long String] NOT LIKE " & chr(34) & _  
"*" & ex4 & "*" & chr(34)  
End if
strsql = strsql & ";"  
End Function