HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMajor

Replace space (" ") with no space ("") in one column

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
spacecolumnwithreplaceone

Problem

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?

Solution

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.

Code Snippets

select trim(kota) from yourtable
update yourtable set kota = trim(kota);

Context

StackExchange Database Administrators Q#54318, answer score: 20

Revisions (0)

No revisions yet.