Click here to Skip to main content
12,763,570 members (32,813 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# MySQL
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 17-Oct-12 22:37pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

"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 :
select * from [tablename] where columnname like 'ab%'
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

you can use mysql Replace function to remove unwanted character from table field and search string. Here is example
select * from [tablename] where Replace(columnname, '-', '') like CONCAT('%',Replace(searchstring, '-', ''), '%')
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

you can do this, first create this method:
public static string GetLike(string input)
    string[] sa = input.Split(' ');
    var sb = new System.Text.StringBuilder();
    foreach (string s in sa)
        sb.Append("%" + s);
    return sb.ToString();

Then, we can use it like this:
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
GenerateSQL("ab cd");

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

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

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

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170217.1 | Last Updated 18 Oct 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100