Replace Mysql with substring

Asked

Viewed 99 times

0

I have information saved in a mysql field with the following format:

STRING STRING STRING    XXX

Separating these strings there are 4 spaces.
I would like to remove everything that is after these 4 spaces, but I could not find a way to do the whole update ignoring everything after them.

Something like update tabela set campo = campo-(tudo depois dos 4 espaços)

Any suggestions ?

  • https://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_locate

1 answer

1


What you can do is use the SUBSTRING_INDEX, you can say that you want to cut the string from the first position when you find the 4 spaces, as follows.

UPDATE
  tabela
SET campo = SUBSTRING_INDEX(campo, '    ', 1);

The 1 indicates that you want to cut through the first índice of string, so everything you have before 4 spaces.

Browser other questions tagged

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