Ask Your Question
3

Is there a way to remove all non-ASCII string values or those in non-English locales from the database in PostgreSQL?

asked 2021-07-09 11:00:00 +0000

huitzilopochtli gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2021-05-24 04:00:00 +0000

pufferfish gravatar image

Yes, you can remove all non-ASCII string values or those in non-English locales from the database in PostgreSQL using the following steps:

  1. First, create a new database backup before performing any deletion operations.

  2. Use the following SQL query to identify all rows with non-ASCII characters in a particular column:

SELECT * FROM my_table WHERE my_column ~ '[^[:ascii:]]';

Replace my_table with the name of your table and my_column with the name of the column you want to search.

  1. To delete all the rows that have non-ASCII characters in a particular column, use the following SQL query:
DELETE FROM my_table WHERE my_column ~ '[^[:ascii:]]';

Again, replace my_table with the name of your table and my_column with the name of the column you want to delete rows from.

  1. In case you want to delete all rows from the table that have non-ASCII characters in any column, you can use the following SQL query:
DELETE FROM my_table WHERE my_table::text ~ '[^[:ascii:]]';

This will convert the entire row to a text value and check for non-ASCII characters before deleting it.

Note: Be careful while deleting rows as this operation is irreversible. It is recommended to create a backup before deleting any data.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-07-09 11:00:00 +0000

Seen: 15 times

Last updated: May 24 '21