SQL Replace when some variable contains semicolons

Asked

Viewed 1,048 times

1

I am using this command smoothly to replace class="texto1" for class="texto2":

UPDATE tabela
SET estilo = REPLACE (estilo,'class="texto1"','class="texto2"')
WHERE estilo LIKE 'class="texto1"';

It works perfectly, but when I want to replace class="texto1" style="width:20px;margin-top:5px that has semicolon ; receive this error message:

#1064 - You have an error in your SQL syntax; check the manual that Corresponds to your Mysql server version for the right syntax to use near 'FROM `table` WHERE 1' at line 1

The command used in the second attempt was this:

UPDATE tabela
SET estilo =
REPLACE (estilo,
         'class="texto1" style="width:20px;margin-top:5px"',
         'class="texto3"')
WHERE estilo LIKE '%class="texto1" style="width:20px;margin-top:5px"%';

What could be causing the mistake?

  • I searched online tools of "Character escape" but none modified the stretch with semicolon.

  • Please specify your question, old text and text1?

  • I would like to replace 'class="texto1" style="width:20px;margin-top:5px' with 'class="texto3' but I get the error shown.

  • @Wesley: is it Mysql or SQL Server? The error message indicates Mysql but the sql-server tag is checked. Could you check?

  • @Josédiz is an sql database running in Lynx by Locaweb, I am layman on the subject, I removed the sql-server tag, I would like to make the change through phpmyadmin

2 answers

1


I managed using command below:

UPDATE `tabela`
SET `estilo` = 'class="text3"'
WHERE `estilo` LIKE 'class="text3" style="width:120px;text-align:center;"'

The error only appears if you click on "Simulate search", but clicking on "Run" will work normally.

  • 1

    Wow, I’ve been racking my brain for two hours, and the answer was this, it makes a mistake just in "Simulate Research". Pretty inconsistency, huh? How to actually believe that the simulation will be equal to the execution?

0

How are you calling this SQL?

If it’s in c# you can mount the string like this:

var sql = string.Format(@"UPDATE tabela SET estilo = REPLACE (estilo, '{0}', '{1}') where estilo LIKE '%class="{2}';", valor1, valor2, valor3);

You can also use a stored Procedure:

CREATE PROCEDURE [dbo].[SubstituiTexto]
    @antigo varchar(50),
    @novo varchar(50),
    @valor varchar(50)
AS
BEGIN
   UPDATE tabela SET estilo = REPLACE (estilo, @antigo, @novo) where estilo LIKE @valor
END

and calls the sp so: exec substituitexto 'texto1','texto2','texto3'

  • 1

    Good evening Roger, sorry, it is the first time I open a question in this area, I forgot to specify, but I would like to run the command through phpmyadmin, I could not use any of the above options

Browser other questions tagged

You are not signed in. Login or sign up in order to post.