How do I remove all spaces from a field in a Postgres database in an update query?
Asked Answered
S

7

68

What would be the proper syntax used to run an update query on a table to remove all spaces from the values in a column?

My table is called users and in the column fullname some values look like 'Adam Noel'. I want to remove the space so that the new value is 'AdamNoel'

I have like 30k rows

Spirt answered 4/12, 2013 at 13:23 Comment(3)
Just do a search replace? Anything particular you find difficult?Summons
I'd try SELECT regexp_replace(myfield, ' ', '', 'g') FROM mytable;Caracara
i did try it but the update amount of raw was more than what i what i have with space i mean space raw is 30k but the amount update was 45k , can we limite the update just for the space and ignore any other special charters – Marco DonJuan just now editSpirt
P
92
update users
  set fullname = replace(fullname, ' ', '');
Protist answered 4/12, 2013 at 13:28 Comment(4)
i did try it but the update amount of raw was more than what i what i have with space i mean space raw is 30k but the amount update was 45k , can we limite the update just for the space and ignore any other special chartersSpirt
@MarcoDonJuan: sorry, you lost me at "space raw". I have no idea what you are talking about. Please create a sample at sqlfiddle.com and let us know how the new data should look like.Protist
ok , ive fullname coulmn data like "Adam John" your statment removed space and its become "AdamJohn" which is pretty much what i want , but its also removed spiceal charters from other names like "mark'bart" "suzan_andy" etcSpirt
@Spirt I tried suzan_andy and mark'bart, replace didn't remove special characters. It works just fine!Sin
M
26

To remove all whitespace (not just space characters) one can use:

update users set fullname = regexp_replace(fullname, '\s', '', 'g');
commit;
Mont answered 28/2, 2018 at 21:9 Comment(0)
K
13

Just use the simple code

 REPLACE('some_string', ' ', '')

or

Replace('some_string', '\s', '')

to remove any white space from the string

Keyboard answered 5/1, 2019 at 17:55 Comment(2)
\s has no special meaning for replace() the second statement removes a backslash followed by the character s from the string.Protist
Sorry, but this answer is partly wrong and doesn't show anything, which has not already been shown in previous answers: (1) There is no difference between REPLACE() and Replace() (2) REPLACE() works, as you suggest, but this has already been shown in 2013 by user330315. (3) REPLACE() doesn't allow REGEX as you suggest by using \sBelfry
F
6

You can include a condition to update only values that need it with the replace.

UPDATE users SET fullname = REPLACE(fullname, ' ', '') WHERE fullname ~* ' ';

Quick and dirty

Fondly answered 15/4, 2020 at 14:28 Comment(2)
Would that be the same as UPDATE users SET fullname = REPLACE(fullname, ' ', '') WHERE fullname not like '% %';Banda
@jeremy : it should be UPDATE users SET fullname = REPLACE(fullname, ' ', '') WHERE fullname like '% %';Bludgeon
T
4

If it's a text[] column, you can do something like this:

UPDATE users SET pets = string_to_array(replace(array_to_string(pets, ';'), ' ', ''), ';');

Before: {"Big Dog", "Small Cat"}

After: {"BigDog", "SmallCat"}

Tapdance answered 9/2, 2017 at 23:25 Comment(0)
B
3

Can perform an update all with the trim function.

UPDATE users AS u SET name = TRIM(u.name)

Optionally add a clause to update only the records that need it, instead of all, but uses more CPU.

UPDATE users AS u SET name = TRIM(u.name) WHERE LENGTH(TRIM(u.name)) <> LENGTH(u.name)

If the table has a unique index on the value being trimmed, you could get a duplicate key error.

Bacchanal answered 7/11, 2019 at 0:31 Comment(1)
PO wants to remove all white spaces, however, trim only remove space from start, end or both. Not remove space in the string.Expansionism
Z
1
UPDATE customers SET first_name = TRIM (TRAILING FROM first_name ) where id = 1

For example, if you want to remove spaces from the beginning of a string, you use the following syntax:

TRIM(LEADING FROM string) The following syntax of the TRIM() function removes all spaces from the end of a string.

TRIM(TRAILING FROM string) And to remove all spaces at the beginning and ending of a string, you use the following syntax:

TRIM(BOTH FROM string)

Zealous answered 23/10, 2019 at 10:35 Comment(1)
It will not convert Adam Noel into AdamNoelBingaman

© 2022 - 2024 — McMap. All rights reserved.