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

前端 未结 7 1646
梦毁少年i
梦毁少年i 2020-12-30 18:58

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

7条回答
  •  感情败类
    2020-12-30 19:39

    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.

提交回复
热议问题