0

I have many fields that contains extra space in start, like:

" RTX 3060"
-^

I want to remove first extra space from all fields of this table.

What I tried:

UPDATE table set field = concat( '', substring(field , 1)) where left(field ,1)=' ';

Return 0 result!

5
  • I think you need 2 on substring insted of one. But still there are couples I can think of UPDATE table set field = concat( '', substring(field , 2)) where field LIKE " %" or UPDATE table set field = substring(field , 2) where field LIKE " %" Commented Aug 18, 2022 at 19:07
  • @RajeshPaudel Backticks format code, asterisks are simply for bold. Commented Aug 18, 2022 at 19:10
  • If you have MySQL 8.0 or better, consider REGEXP_REPLACE. Commented Aug 18, 2022 at 19:11
  • 1
    @tadman thanks I have a habit of putting three backticks maybe that's why it wasn't working. I realised i could put one instead of three. Thank You Commented Aug 18, 2022 at 19:12
  • 1
    @RajeshPaudel One for inline, three for block form, which I believe only works in answers, etc. Commented Aug 18, 2022 at 19:13

2 Answers 2

2

Try with this statement, it will remove only the first space char of field is it's a space:

UPDATE `table` 
SET column = SUBSTRING(column,2)
WHERE SUBSTRING(column, 1, 1) = ' ';
Sign up to request clarification or add additional context in comments.

Comments

0

Try using the LTRIM function. The syntax looks something like this:

UPDATE table SET column = LTRIM(column);

2 Comments

LTRIM remove ALL first chars, the question is about removing the first extra space, not all.
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.