2019-05-07
How can I use LEFT and REPLACE on the same column in mysql?
源自: stackoverflow
问题

I want to select a column and remove(replace with space) any line feeds or carriage returns and I want to get only the first 40 characters in the column after the replace.

Here is the query I tried but I get

select date, REPLACE(LEFT(message, '\r', ' '), 30)
from table1 
where message like 'testing%'

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ' ' '), 30) from events_cleared where message like 'testing%' ' at line 1

答案
1

You have the ordering of your arguments incorrect (you have passed the REPLACE arguments to LEFT). Try this:

select date, REPLACE(LEFT(message, 30), '\r', ' ')
from table1 
where message like 'testing%'
How can I use LEFT and REPLACE on the same column in mysql?
查看原网页