user2710234 user2710234 - 6 months ago 27
SQL Question

SQL Select query where column like string with and without spaces

I have a SQL Select query in PHP where i need to lookup rows in a table where a column is like a string with and without spaces

for example, if my value in the database is

ABC 123
and i search for
ABC123
the query will be

$sql = SELECT * from table where col_name LIKE '%ABC123%' ";


but i need it to return the row where col_name equals ABC 123

and vice versa, for example, if the database value is
123ABC
and i search for
123 ABC
the query will be

$sql = SELECT * from table where col_name LIKE '%123 ABC%' ";


and i need it to return the row where col_name equals
123ABC

Answer
$sql = SELECT * from table where REPLACE(col_name,' ','') LIKE '%ABC123%' ";
Comments