Search fails when criteria includes an underscore

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Claus Mygind
    Contributor
    • Mar 2008
    • 571

    Search fails when criteria includes an underscore

    The following where clause returns data not wanted.
    Code:
    select * from <someTable> where <Column1> like "%Lamp_I%" or <Column2> like "%Lamp_I%"
    My sample data should have returned one row but instead it returned all rows whether it had an underscore or a blank.

    sample data looked like this

    12 records had the following "Lamp Inc." in <Column1>

    one of the 12 records contained "Lamp_I" in <Column2>

    How can I prevent returning records that are not like the criteria submitted when an underscore is included?
  • Anas Mosaad
    New Member
    • Jan 2013
    • 185

    #2
    Claus,
    I'm not sure I'm understanding the question correctly or not. However, underscore is a special character in MySQL that mean any character (one single character). To escape it, prefix it with \ character (i.e. "%Lamp\_I%" )

    Comment

    • Claus Mygind
      Contributor
      • Mar 2008
      • 571

      #3
      You understood me perfectly. That is what I needed to know. Thanks

      Comment

      • Anas Mosaad
        New Member
        • Jan 2013
        • 185

        #4
        You are always welcome :-)

        Comment

        Working...