db2 删除所有非字母数字字符,包括不可打印字符和特殊字符

db2 remove all non-alphanumeric, including non-printable, and special characters

这听起来像是重复,但现有的解决方案不起作用。 我需要从 varchar 字段中删除所有非字母数字。我正在使用以下内容,但并非在所有情况下都有效(它适用于菱形问号字符):

  select TRANSLATE(FIELDNAME, '?',
                 TRANSLATE(FIELDNAME , '', 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789')) 
   from TABLENAME

它所做的是内部翻译解析所有非字母数字字符,然后外部翻译将它们全部替换为“?”。这似乎适用于替换角色。但是,它会抛出 The second, third or fourth argument of the TRANSLATE scalar function is incorrect.,根据 IBM 的预期:

The TRANSLATE scalar function does not allow replacement of a character by another character which is encoded using a different number of bytes. The second and third arguments of the TRANSLATE scalar function must end with correctly formed characters.

有没有办法解决这个问题?

编辑:@Paul Vernon 的解决方案似乎有效:

· 6005308      ??6005308
–6009908       ?6009908
–6011177       ?6011177
��6011183�� ??6011183??

尝试regexp_replace(c,'[^\w\d]','')regexp_replace(c,'[^a-zA-Z\d]','')

例如

select regexp_replace(c,'[^a-zA-Z\d]','') from table(values('AB_- C$£abc�3£')) t(c)

哪个returns

1
---------
ABCabc123

顺便说一句,请注意,允许的正则表达式模式列在此页面上 Regular expression control characters

在集合之外,以下内容必须以反斜杠开头才能被视为文字

* ? + [ ( ) { } ^ $ | \ . /

在集合中,后面必须以反斜杠开头才能被视为文字

Characters that must be quoted to be treated as literals are [ ] \ Characters that might need to be quoted, depending on the context are - &