SQL Wildcard Problem

Posted on 2004-11-04
Last Modified: 2008-02-01
I am using OLEDB with VB.NET and I'm trying to make a like statement that will pull all records from an Access database that does not start with a letter. This works in Access: LIKE '#*'

I know the wildcards are different for ADO so I have tried these:
LIKE '[^A-Z]%'
LIKE '#%'  <--  Gives no error but no resultes eather. There are no records that start with the # symbol.

I even tried these and they errored
LIKE '[0123456789]%'
LIKE '[38]%'

Are brackes not supported or am I just missing the obvious?

One of my resources:
Question by:belthasar
    LVL 26

    Expert Comment

    Don't know access very well,
    but you could try

    WHERE ISNUMERIC(LEFT(<yourcolumn>, 1))
    WHERE ISNUMERIC(LEFT(<yourcolumn>, 1)) = 1 (or true ?)
    LVL 4

    Expert Comment

    This looks right to me:  LIKE '[^A-Z]%'  or NOT LIKE '[A-Z]%'

    but since it's not working you can always try this instead:

    LEFT(yourfield,1) NOT BETWEEN 'A' AND 'Z'

    or LEFT(yourfield,1) NOT LIKE '[A-Z]'  maybe...

    Author Comment

    LEFT(yourfield,1) NOT LIKE '[A-Z]' gives me: The expression contains undefined function call LEFT().
    NOT LIKE '[A-Z]%' gives me: Error in Like operator: the string pattern '[A-Z]%' is invalid.

    Heres my code:
    Dim ListingData As New DataSet("Listings")
    ListingData = Query("SELECT * FROM Directory ORDER BY company1")
    Dim LikeListings As DataRow()
    LikeListings = ListingData.Tables(0).Select("company1 NOT LIKE '[A-Z]%'")

    However, this works like pie to get all that start with A:
    LikeListings = ListingData.Tables(0).Select("company1 NOT LIKE 'A%'")
    LVL 4

    Accepted Solution

    Try MID(company,1,1) instead of LEFT() or SUBSTR(company,1,1).

    Also instead of the between or isnumeric() you could try

    LVL 15

    Expert Comment

    Hi belthasar.

    Does this do what you want?  I think perhaps you need the square brackets in access, this certainly works as an access query

    SELECT d.*
    FROM Directory AS d
    WHERE Left([company1],1) Not Like "[a-Z]";

    Author Comment

    It's not liking MID, INSTR, or SUBSTR eather, but SUBSTRING works. So I tried this:
    SUBSTRING(company1,1,1) NOT LIKE '[A-Z]'
    and got: Error in Like operator: the string pattern '[A-Z]' is invalid.

    I found this:
    It dosent look like brackets are supported.

    Author Comment

    OK, I know thisis sloppy, but it works:

    Dim filter As String
    Dim ascii As Short
    For ascii = 65 To 90
          If filter <> "" Then filter &= " AND "
          filter &= "SUBSTRING(company1,1,1) NOT LIKE '" & Chr(ascii) & "'"
    LikeListings = ListingData.Tables(0).Select(filter)

    LVL 4

    Expert Comment

    Seems SQL syntax for Jet ADO is somewhat of a mystery...

    This ought to work:

    SUBSTRING(company,1,1) NOT BETWEEN 'A' AND 'Z'

    Author Comment

    LVL 4

    Expert Comment

    I didn't realize you were using .NET

    How about:

    CONVERT(SUBSTRING(company,1,1),'System.Int32') > 0

    Of course this only returns values starting with numbers... don't know if you have other characters.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
    Read about achieving the basic levels of HRIS security in the workplace.
    Video by: Steve
    Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
    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…

    779 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

    15 Experts available now in Live!

    Get 1:1 Help Now