How to get values separated by "<BR>" in Mysql?

Asked

Viewed 1,172 times

1

The idea is simple...I need to get values from a table (by performing SELECT) that are separated by <BR> of a STRING field and then store in a table, inserting each separation, a record.

Example 1:

Registering:

"Enviado para Cotação por Nemoreni Oliveira em 03-12-2013 13:46:22<br>Cotado por Edimar Nunes Pereira em 03-01-2014 13:52:14<br>"

Expected registration:

"Enviado para Cotação por Nemoreni Oliveira em 03-12-2013 13:46:22"
"Cotado por Edimar Nunes Pereira em 03-01-2014 13:52:14"

Example 2:

Registering:

"Enviado para Cotação por Rosimar da Silva Ribeiro em 09-12-2013 13:17:13<br>Cotado por Edimar Nunes Pereira em 09-12-2013 16:26:21<br><br>Reavaliado por: Edimar Nunes Pereira. Em: 2013-12-27 16:45:05<br>Motivo: incluir o ultimo item da NF na sc  de compra.<br><br>Enviado para Cotação por Rosimar da Silva Ribeiro em 27-12-2013 16:59:39<br>Enviado para Cotação por Rosimar da Silva Ribeiro em 27-12-2013 17:00:24<br>Cotado por Edimar Nunes Pereira em 30-12-2013 08:14:26<br>Enviado para Cotação por Rosimar da Silva Ribeiro em 02-01-2014 14:03:27<br>Cotado por Edimar Nunes Pereira      em 02-01-2014 14:05:23<br>"

Expected registration:

"Enviado para Cotação por Rosimar da Silva Ribeiro em 09-12-2013 13:17:13"
"Cotado por Edimar Nunes Pereira em 09-12-2013 16:26:21"
"Reavaliado por: Edimar Nunes Pereira. Em: 2013-12-27 16:45:05"
"Motivo: incluir o ultimo item da NF na sc  de compra."
"Enviado para Cotação por Rosimar da Silva Ribeiro em 27-12-2013 16:59:39"
"Enviado para Cotação por Rosimar da Silva Ribeiro em 27-12-2013 17:00:24"
"Cotado por Edimar Nunes Pereira em 30-12-2013 08:14:26"
"Enviado para Cotação por Rosimar da Silva Ribeiro em 02-01-2014 14:03:27"
"Cotado por Edimar Nunes Pereira em 02-01-2014 14:05:23"
  • The question is confusing, but looking at its original title occurred to me as follows: you want Mysql to return line breaks replaced by <br>, that’s it?

  • It is still a little confused, have as an example the select that you have today to try to understand a little better the situation?

  • It is mandatory to do this by the bank or it can be done by some other language?

  • The separation is in the STRING part "<BR>", so I need to get the data that are between them. When I posted the question, the "<BR>" was excluded for some reason.

  • And how do I store these records in a temporary table on separate lines?

4 answers

4


Replace line breaks with <br/>

You can use the function REPLACE for this. Example:

select REPLACE(campo,'\n','<br />') from tabela

Note that I used \n as line separator, however this may vary depending on the operating system that sends the data to Mysql.

Demo on the Sqlfiddle

Split content by breaks

In Mysql there is no function SPLIT or something similar that can transfer a single column into several.

There are, however, some palliative solutions with the use of commands with SUBSTRING, LOCATE and SUBSTRING_INDEX, as described in this SOEN question and also us comments from the Mysql documentation.

You can create a precedent that looks for the first occurrence of line breaking, grab the text before it and add it where you want. Put this in a loop and do it until there are no more items left.

I made a basic example:

CREATE procedure split(texto text)
begin
    declare trecho text;
    declare pos int;
    declare pos_ant int;
    set pos = LOCATE('\n', texto);
    set pos_ant = 1;
    delete from linhas;
    while pos > 0 do

       set trecho = substring(texto, pos_ant, pos - pos_ant);
       if length(trim(trecho)) > 0 then
          insert into linhas (linha) values (trecho);
       end if;

       set pos_ant = pos + 1;
       set pos = LOCATE('\n', texto, pos_ant);

    end while;
    if pos_ant <= length(texto) then
        set trecho = substring(texto, pos_ant, length(texto) - pos_ant + 1);
        if length(trim(trecho)) > 0 then
            insert into linhas (linha) values (trecho);
        end if;
    end if;
end//

Check on the Sqlfiddle

After seeing the update in the question, I made a new example using <br>:

CREATE procedure split(texto text)
begin
    declare trecho text;
    declare pos int;
    declare pos_ant int;
    set pos = LOCATE('<br>', texto);
    set pos_ant = 1;
    delete from linhas;
    while pos > 0 do

       set trecho = substring(texto, pos_ant, pos - pos_ant);
       if length(trim(trecho)) > 0 then
          insert into linhas (linha) values (trecho);
       end if;

       set pos_ant = pos + 4;
       set pos = LOCATE('<br>', texto, pos_ant);

    end while;
    if pos_ant <= length(texto) then
        set trecho = substring(texto, pos_ant, length(texto) - pos_ant + 1);
        if length(trim(trecho)) > 0 then
            insert into linhas (linha) values (trecho);
        end if;
    end if;
end//

Check on the Sqlfiddle

Considerations

This type of treatment is best done in a complete programming language like Java, C#, PHP, Python and so on. Perhaps it is better to create a small program to take charge of the task.

  • Perfect! Like a glove! Thank you very much!

  • @mcardoso Cool! If you solved your problem, be sure to mark the answer as correct.

  • @mcardoso I saw that there were some bugs in the routines related to recovering the appropriate chunk of text. I just fixed them.

2

Exchange the <br> for \n using the function replace() mysql

SELECT replace(campo, '<br>', '\n') FROM tabela
  • blz! And how do I store these records in a temporary table in separate rows?

  • @mcardoso, I think you’ll have to script in some programming language to make this return exactly the way you want it.

1

If you can create a file on the database server, use SELECT ... INTO

SELECT ...
FROM ...
INTO OUTFILE '/tmp/...'
LINES TERMINATED BY '<br>\n'

Then use the file as input for inserts with LOAD DATA INFILE

LOAD DATA INFILE '/tmp/...'
INTO ...

Warning: method not tested

1

In the SELECT, you can concatenate to the column value a string that your application understands as line break (<br /> or \n).

In mysql you can do this with CONCAT()

    SELECT CONCAT(a.nome, '<br />') as nome from registros a;

Browser other questions tagged

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