Using LIKE Operator

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • suganya
    New Member
    • Dec 2006
    • 39

    Using LIKE Operator

    Hi

    If I retrive the data using the query

    select Name from DoctorRegistrat ion3 where Country='Indone sia' or AreaOfSpecializ ation='Cardiolo gy' or Name like 'i%'


    the answer is

    Name
    -----------
    Imran
    Sabri
    Ghan


    If I retrive the same query in .net by giving the same corresponding data as

    "select Name from DoctorRegistrat ion3 where Country='" + ddlCountry.Sele ctedItem.Text.T oString() + "' or AreaOfSpecializ ation='" + ddlDisease.Sele ctedItem.Text.T oString() + "' or Name like ''" + txtName.Text + "'%'";

    then it shows the following error

    Incorrect syntax near 'i'.

    can anybody help me how to use the LIKE operator in .net
  • deepuv04
    Recognized Expert New Member
    • Nov 2007
    • 227

    #2
    Hi,

    Use the following statement probably will work.

    "select Name from DoctorRegistrat ion3 where Country='" + ddlCountry.Sele ctedItem.Text.T oString() + "' or AreaOfSpecializ ation='" + ddlDisease.Sele ctedItem.Text.T oString() + "' or Name like '" + txtName.Text + "%'";

    Comment

    • ck9663
      Recognized Expert Specialist
      • Jun 2007
      • 2878

      #3
      If deepuv's suggestion did not work for you, try displaying (using a prompt/messagebox or as text on your form) the entire query string as text. Copy it and paste it on your query analyzer/window. Then execute it. Your error would be more visible there.

      Happy Coding.

      -- CK

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        Could this be caused by the extra ' in front of the % in the LIKE clause?

        Comment

        • Delerna
          Recognized Expert Top Contributor
          • Jan 2008
          • 1134

          #5
          Definitely, that part of the query would read as
          [code=sql]
          or Name like 'i''%' incorrect syntax near i
          [/code]
          instead of
          [code=sql]
          or Name like 'i%' correct
          [/code]

          Comment

          Working...