none
datatable select - part of the string RRS feed

  • Question

  • Hi, I was looking for a lot of posts in this forum, but no help.
    I have set of names in datatable and to find a part of them *xxx* and show it in datagrid.
    Datatable info, columns names.
    I want to find all names with geo (george, asgeopet, ...)
    info.select("names = *geo*").
    Can anyone show me the right select? Thanks very mutch,
    Tuesday, February 5, 2008 11:20 AM

Answers

  • Hello,

     

    which is the engine of your database? ( SQL Server, ACCESS,Oracle..)

     

    Normally, you may use something like :

    SELECT field1,field2,... FROM tablename WHERE fieldi LIKE 'geo%'

    but the exact syntax is different , depending of the engine ( what i wrote is working for SQL Server )

     

    you could use  ( in VC# )

    String str = "names LIKE '%geo%'";

    DataRows[] rows = info.Select(str);

    see :

    http://msdn2.microsoft.com/en-us/library/det4aw50(VS.80).aspx

     

    I hope that will help you

     

    Have a nice day

     

     

    Tuesday, February 5, 2008 2:49 PM
  •  

    An addition to Papy's post. Both * and % wildcards supported inside of Select expressions and they do exact same job. You also need to keep in mind that value between wildcards could contain single quotes (like %ab’cd%’). In this case you need to double those quotes. In general it is good idea to double single quotes inside of the string anyway and your code would look like

    string myValue = "Some Value Here".Replace("'","''");

    info.Select("[names] = %'" + myValue +  "%'");

    Tuesday, February 5, 2008 11:42 PM
    Moderator

All replies

  • Hello,

     

    which is the engine of your database? ( SQL Server, ACCESS,Oracle..)

     

    Normally, you may use something like :

    SELECT field1,field2,... FROM tablename WHERE fieldi LIKE 'geo%'

    but the exact syntax is different , depending of the engine ( what i wrote is working for SQL Server )

     

    you could use  ( in VC# )

    String str = "names LIKE '%geo%'";

    DataRows[] rows = info.Select(str);

    see :

    http://msdn2.microsoft.com/en-us/library/det4aw50(VS.80).aspx

     

    I hope that will help you

     

    Have a nice day

     

     

    Tuesday, February 5, 2008 2:49 PM
  •  

    An addition to Papy's post. Both * and % wildcards supported inside of Select expressions and they do exact same job. You also need to keep in mind that value between wildcards could contain single quotes (like %ab’cd%’). In this case you need to double those quotes. In general it is good idea to double single quotes inside of the string anyway and your code would look like

    string myValue = "Some Value Here".Replace("'","''");

    info.Select("[names] = %'" + myValue +  "%'");

    Tuesday, February 5, 2008 11:42 PM
    Moderator
  • Hello,

     

    You are absolutely right. It's especially the case with the languages which are using ' like in the name O'Kelly.

    I are answered too quickly.

    I've already answered to a similar question and that time i have added " with double simple quotes" as double simple quotes and simple double quotes are easily differentiated

     

    Have a nice day

     

     

    Wednesday, February 6, 2008 6:02 AM