Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

postgresql

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

like image 776
LeoSam Avatar asked Dec 04 '13 13:12

LeoSam


People also ask

How do I trim a column in PostgreSQL?

PostgreSQL provides you with LTRIM, RTRIM() and BTRIM functions that are the shorter version of the TRIM() function. The LTRIM() function removes all characters, spaces by default, from the beginning of a string. The RTRIM() function removes all characters, spaces by default, from the end of a string.

How do I change the space in PostgreSQL?

In PostgreSQL, the TRIM() function is used to remove the longest string consisting of spaces or any specified character from a string. By default, the TRIM() function removes all spaces (' ') if not specified explicitly.

Can Postgres columns have spaces?

It is acceptable to use spaces when you are aliasing a column name. However, it is not generally good practice to use spaces when you are aliasing a table name. The alias_name is only valid within the scope of the SQL statement.

What is Btrim in PostgreSQL?

BTRIM() function The PostgreSQL btrim function is used to remove the longest string specified in the argument from the start and end of the given string. If no string for removing default space will be removed from leading and trailing side from the string.


2 Answers

update users   set fullname = replace(fullname, ' ', ''); 
like image 146
a_horse_with_no_name Avatar answered Oct 12 '22 18:10

a_horse_with_no_name


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

update users set fullname = regexp_replace(fullname, '\s', '', 'g'); commit; 
like image 45
Rasjid Wilcox Avatar answered Oct 12 '22 19:10

Rasjid Wilcox