You can use the following:
SELECT *, REPLACE(column_name, '_', '') AS new_value
FROM table_name
WHERE REPLACE(column_name, '_', '') LIKE '120515%';
demo on dbfiddle.uk
You can't use the column alias on the WHERE
part of your query because the SELECT
part is executed after the WHERE
part. See the following StackOverflow answer describing the query execution order of MySQL.
So you have to use the REPLACE
on the WHERE
part. In case you don't need to see / use the new_value
you can remove the REPLACE
on SELECT
.
In case the format is always xxxx_xx_xx
, you can also use a solution like the following:
SELECT *
FROM table_name
WHERE column_name LIKE CONCAT(INSERT(INSERT('120515', 5, 0, '_'), 8, 0, '_'), '%');
demo on dbfiddle.uk
As @Madhur Bhaiya already mentioned in the comments, you can use a index on the column_name
to improve the performance on a large table.