locked
syntax for "LIKE" in parameterized query RRS feed

  • Question

  • User-1242214802 posted

    Hi All: I have a datatable in a dataset that I want to add a LIKE query to, but I can't seem to get the syntax right.

    I'm trying 

    SELECT  lastName
    FROM   members
    WHERE  lastName LIKE  @lname + '%'

    but it's not returning any results unless I enter the full name...what am I missing?

    I also tried

    WHERE  lastName LIKE  (@lname + '%')

    and

    WHERE  lastName LIKE ('' + @lname + '%')

    Thursday, July 17, 2014 9:32 AM

Answers

  • User281315223 posted

    The query you are essentially using would be considered a "Starts-With" query as you are only using the wildcard character '%' to match contents after your parameter :

    SELECT lastName
      FROM members
     WHERE lastName 
      LIKE @lname + '%'

    You can see a full example here using the following data :

    and executing the following query :

    -- Define your parameter (example)
    DECLARE @lname AS VARCHAR(64)
    SET @lname = 'Mc'
    
    SELECT lastName
      FROM members
     WHERE lastName 
      LIKE @lname + '%'

    yields :

    This code should be correct.

    Could you post an example of the code that you are currently using or how you are adding these parameters?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 17, 2014 10:29 AM
  • User1918509225 posted

    Hi dotnetnoob,

    For your problem,i write  a sample for:

                string connstring = "xx";
                SqlConnection conn = new SqlConnection(connstring);
                conn.Open();
                 string name = "ke";
               //if you want to find UserName Column'value contains  "ke", try the below
                string sql = "select * from Employee Where UserName like '%"+name+"%' ";
    
              //if you want to find UserName Column'value start with"ke", try the below
                string sql = "select * from Employee Where UserName like '"+name+"%' ";
    
                SqlCommand cmd = new SqlCommand(sql, conn);
                 SqlDataAdapter sda = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                sda.Fill(ds, "aa");
                GridView1.DataSource = ds;
                GridView1.DataBind();

    Best Regards,
    Kevin Shen.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 17, 2014 11:31 PM
  • User-1569093213 posted
    To Search Lastname Starting with Chracter  @lname
    "Select lastName" From members Where lastname LIKE '"+@lname+"%'";
    
    To search Lastname Which Contains the Character @lname
    "Select lastname From members where lastname like '%"+@lname+"%'";
    
    To Search lastname Which Ends With Chracter @lname
    "Select lastname From members where lastname like '%"+@lname+"%'";
    

    Hope this will help you

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 18, 2014 12:28 AM

All replies

  • User-1360095595 posted

    That looks right. If you replace @lname by a hardcoded value, does it work?

    How are you specifying the @lname parameter? 

    Thursday, July 17, 2014 10:10 AM
  • User281315223 posted

    The query you are essentially using would be considered a "Starts-With" query as you are only using the wildcard character '%' to match contents after your parameter :

    SELECT lastName
      FROM members
     WHERE lastName 
      LIKE @lname + '%'

    You can see a full example here using the following data :

    and executing the following query :

    -- Define your parameter (example)
    DECLARE @lname AS VARCHAR(64)
    SET @lname = 'Mc'
    
    SELECT lastName
      FROM members
     WHERE lastName 
      LIKE @lname + '%'

    yields :

    This code should be correct.

    Could you post an example of the code that you are currently using or how you are adding these parameters?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 17, 2014 10:29 AM
  • User1918509225 posted

    Hi dotnetnoob,

    For your problem,i write  a sample for:

                string connstring = "xx";
                SqlConnection conn = new SqlConnection(connstring);
                conn.Open();
                 string name = "ke";
               //if you want to find UserName Column'value contains  "ke", try the below
                string sql = "select * from Employee Where UserName like '%"+name+"%' ";
    
              //if you want to find UserName Column'value start with"ke", try the below
                string sql = "select * from Employee Where UserName like '"+name+"%' ";
    
                SqlCommand cmd = new SqlCommand(sql, conn);
                 SqlDataAdapter sda = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                sda.Fill(ds, "aa");
                GridView1.DataSource = ds;
                GridView1.DataBind();

    Best Regards,
    Kevin Shen.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 17, 2014 11:31 PM
  • User-1569093213 posted
    To Search Lastname Starting with Chracter  @lname
    "Select lastName" From members Where lastname LIKE '"+@lname+"%'";
    
    To search Lastname Which Contains the Character @lname
    "Select lastname From members where lastname like '%"+@lname+"%'";
    
    To Search lastname Which Ends With Chracter @lname
    "Select lastname From members where lastname like '%"+@lname+"%'";
    

    Hope this will help you

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 18, 2014 12:28 AM