I have a table like this:

ID | Propinsi | Kota |
 1 | Aceh     | Denpasar
 2 | Aceh     | Banda Aceh
 3 | Sumatera | Asahan

This table has many rows. The problem is I want to replace the space before the text in column Kota for all rows like this:

ID | Propinsi | Kota |
 1 | Aceh     |Denpasar
 2 | Aceh     |Banda Aceh
 3 | Sumatera |Asahan

I searched Google, the function replace in MySQL only affects one row:

SELECT REPLACE(string_column, 'search', 'replace') as Kota

Can someone fix my problem?


4 Answers 4


Try this:

To show without space:

select trim(kota) from yourtable

To change your data:

update yourtable set kota = trim(kota);

TRIM function is different to REPLACE. REPLACE substitutes all occurrences of a string; TRIM removes only the spaces at the start and end of your string.

If you want to remove only from the start you can use LTRIM instead. For the end only you can use RTRIM.


Run New Query in mysql

select REPLACE(kota,' ','') from table-name

this will show the result how it looks like after trimming spaces from the column and to update

update table-name set kota = REPLACE(kota,' ','')


  • 2
    The asker only wants to "Replace the Space berfore text on column Kota for All ROw". Your solution will replace all spaces, not just the leading ones. Jul 4, 2016 at 9:28
        /*in Sql remove extra space b/w string*/
SELECT REPLACE(@Variable, ' ', '')


          /*in Sql remove extra space b/w string*/
        DECLARE @str varchar(150)
        SET @str='Hello    Welcome   to   stackover     flow '
        Select REPLACE(REPLACE(REPLACE(@str,' ','{}'),'}{',''),'{}',' ')

    /*in c#*/
     // Mysample string
                string str ="hi you           are          a demo";

                //Split the words based on white sapce
                var demo= str .Split(' ').Where(s => !string.IsNullOrWhiteSpace(s));

                //Join the values back and add a single space in between
                        str = string.Join(" ", demo);

    //output: string str ="hi you are a demo";
  • Please explain how your query solves the author's problem; answers without explanation generally aren't received well.
    – Glorfindel
    May 17, 2019 at 6:56
UPDATE yourtable SET kota = TRIM(BOTH ' ' from kota);
  • 2
    How is this different from the accepted answer? Feb 27, 2018 at 6:55

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.