如何用空值替换非ascii字符

问题描述 投票:0回答:1

如何在postgresql中用空值替换非ascii字符

table :Emp
address
Îlt-t-Fce
ÄddÄ« ÄrkÊ¿ay
ʿAlūla

根据以上数据,我想输出如下

Address
Ilt-t-Fce
AddAArkEay
EAlAla

我尝试过如下

SELECT replace(addres,'%[^a-Z0-9, ]%',' ')
FROM emp
WHERE address like '%[^a-Z0-9, ]%'
or 
select   replace(addres,'^[^[:ascii:]]',' ') from  emp where  address ~ '^[^[:ascii:]]' 

以上两个queris没有给出预期的结果你能告诉我如何在postgresql中用空值替换非ascci chareater的查询

postgresql postgresql-9.4
1个回答
0
投票

在第一个查询中,您尝试将LIKE与正则表达式一起使用,但这是不可能的,因此您要排除要替换字符的结果。有关详细信息,请参阅SIMILAR TOhttps://www.postgresql.org/docs/9.4/functions-matching.html

此外,replace函数使用字符串但不接受正则表达式。你必须使用regexp_replace。见https://www.postgresql.org/docs/9.4/functions-string.html

您的解决方案是:

SELECT regexp_replace(address, '[^[:ascii:]]', '', 'g');
FROM emp
WHERE address SIMILAR TO '%[^[:ascii:]]%';

g标志意味着全局,因此它将取代每次出现。如果省略它,它将仅删除第一次出现。

其他例子:

要排除重音字符:

select regexp_replace(address, '[À-ÿ]', '', 'g');

要排除非字母数字字符:

select regexp_replace(address, '[^[:alnum:]]', '', 'g');
© www.soinside.com 2019 - 2024. All rights reserved.