How to avoid wildcard characters in query

Posted on 2006-03-30
Last Modified: 2012-06-21

I am trying to avoid (or somehow convert to string) wildcard characters in my query for example (only end of query):

WHERE searchstring LIKE '% 123_ %'

is it possible to somehow sql treat 123_ as string and not to treat character _ as wildcard.

I am using:
sSql = sSql + " AND user LIKE '%" & Text1.Text & "%'"

now when in Text1.Text is found square bracket character [ it produces error, how can I solve this problem?

Question by:damirsel
    LVL 38

    Expert Comment

    by:Jim P.
    This an ADP?

    Then it would be
     WHERE searchstring LIKE '% 123\_ %' escape '\'

    Author Comment

    sorry don't understand your question "ADP?"

    this is not working it pops up error message: 'syntax error missing operator...'
    LVL 38

    Expert Comment

    by:Jim P.
    Is this an Access Data Project connected to a SQL server?

    Then the wildcards are percent "%" and  underscore "_".  If this is linked tables then the wildcards are the asterick "*" and question mark "?" and you don't have to worry about the _.

    Author Comment

    I am using VB with ADO (Microsoft.Jet.OLEDB.4.0) to connect to mdb.
    LVL 38

    Expert Comment

    by:Jim P.
    >>I am using VB with ADO (Microsoft.Jet.OLEDB.4.0) to connect to mdb.

    Then it is

    sSql = sSql + " AND user LIKE '*" & Text1.Text & "*'"
    LVL 1

    Accepted Solution

    if you want to use a wildcard to look for, as i want to look for  a _, then you have to place the wildcard in square brackets: so like "*123[_]*"

    Good luck, Marc
    LVL 38

    Assisted Solution

    by:Jim P.
    From the VB Help:
    Wildcard Characters Used in String Comparisons

    Built-in pattern matching provides a versatile tool for making string comparisons. The following table shows the wildcard characters you can use with the Like operator and the number of digits or strings they match.

    Character(s) in pattern Matches in expression
    ?       Any single character
    *       Zero or more characters
    #       Any single digit (09)
    [charlist]       Any single character in charlist
    [!charlist]       Any single character not in charlist

    A group of one or more characters (charlist) enclosed in brackets ([ ]) can be used to match any single character in expression and can include almost any characters in the ANSI character set, including digits. In fact, the special characters opening bracket ([ ), question mark (?), number sign (#), and asterisk (*) can be used to match themselves directly only if enclosed in brackets. The closing bracket ( ]) can't be used within a group to match itself, but it can be used outside a group as an individual character.


    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
    In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
    In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now