Like query syntax question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Craig Stadler

    #1

    Like query syntax question

    Im trying to write a like query

    select * from table1 where column1 like 'abc[0-9]%'
    (I realize this syntax isnt correct)

    I want to include any chars from 0-9 in that spot...
    Does anyone know how to do this?

    -Craig
    cstadler18@hotm ail.com


  • steve

    #2
    Re: Like query syntax question

    Originally posted by Craig Stadler
    Im trying to write a like query

    select * from table1 where column1 like ’abc[0-9]%’
    (I realize this syntax isnt correct)

    I want to include any chars from 0-9 in that spot...
    Does anyone know how to do this?

    -Craig
    cstadler18@hotm ail.com
    Craig,

    The elegant way of solving your problem is with regular expressions.
    Search for "mysql regex" using a search engine.

    Otherwise you have to write:

    like ’%abc1%’ or like ’%abc2%’ ....

    yikes!

    --
    http://www.dbForumz.com/ This article was posted by author's request
    Articles individually checked for conformance to usenet standards
    Topic URL: http://www.dbForumz.com/mySQL-query-...ict140719.html
    Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=471490

    Comment

    • Bill Karwin

      #3
      Re: Like query syntax question

      Craig Stadler wrote:[color=blue]
      > Im trying to write a like query
      >
      > select * from table1 where column1 like 'abc[0-9]%'
      > (I realize this syntax isnt correct)
      >
      > I want to include any chars from 0-9 in that spot...
      > Does anyone know how to do this?[/color]

      Standard SQL expressions used with the LIKE predicate have only two
      wildcards: '_' matches a single character (like '.' in UNIX-style
      regular expressions), and '%' matches any sequence of characters (like
      '.*' in UNIX-style regular expressions)

      MySQL extends SQL with a predicate that matches more complex regular
      expressions, including many of the wildcards familiar to UNIX users.

      In your example, you should be able to get the result you want with the
      following query:
      select * from table1 where column1 REGEXP '^abc[0-9]'

      See also:
      http://dev.mysql.com/doc/mysql/en/Pattern_matching.html
      http://dev.mysql.com/doc/mysql/en/Regexp.html

      Regards,
      Bill K.

      Comment

      Working...