Click here to Skip to main content
15,894,405 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a column in mysql named parcel_number of varchar type. It contains values like ab-cd-ef and uvwxyz. A search for abcdef or uv-wx-yz does not return those records.

How should I write the query to meet this requirement?
Posted

"ab-cd-ef" and "abcdef" are different values so you can not search for one and expect the other.

To get both values you can do something like using the LIKE keyword :
SQL
select * from [tablename] where columnname like 'ab%'
 
Share this answer
 
you can use mysql Replace function to remove unwanted character from table field and search string. Here is example
SQL
select * from [tablename] where Replace(columnname, '-', '') like CONCAT('%',Replace(searchstring, '-', ''), '%')
 
Share this answer
 
v2
you can do this, first create this method:
C#
public static string GetLike(string input)
{
    string[] sa = input.Split(' ');
    var sb = new System.Text.StringBuilder();
    sb.Append("'");
    foreach (string s in sa)
    {
        sb.Append("%" + s);
    }
    sb.Append("%'");
    return sb.ToString();
}

Then, we can use it like this:
C#
private string GenerateSQL(string searchThis)
{
    string sql = "select * from `tablename` where `columnname` like "
                 + GetLike(searchThis) + ";";
}

In this case, if you want to search "ab-cd-ef" and "abcdef", you do it like this
C#
GenerateSQL("ab cd");

and this will be generated:
SQL
select * from `tablename` where `columnname` like '%ab%cd%';

you will be able to get both. (ab-cd-ef and abcdef)
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900