db2除去所有非字母数字,包括不可打印的和特殊字符 [英] db2 remove all non-alphanumeric, including non-printable, and special characters

查看:297
本文介绍了db2除去所有非字母数字,包括不可打印的和特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This may sound like a duplicate, but existing solutions does not work. I need to remove all non-alphanumerics from a varchar field. I'm using the following but it doesn't work in all cases (it works with diamond questionmark characters):

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

它的作用是内部翻译解析所有非字母数字字符,然后外部翻译将所有字符替换为'?'。这似乎适用于替换字符。但是,它抛出 TRANSLATE标量函数的第二个,第三个或第四个参数是错误的。根据IBM的预期:

What it's doing is the inner translate parse all non-alphanumeric characters, then the outer translate replace them all with a '?'. This seems to work for replacement character�. However, it throws The second, third or fourth argument of the TRANSLATE scalar function is incorrect. which is expected according to IBM:

TRANSLATE标量函数不允许将一个字符替换为使用不同字节数编码的另一个字符。 TRANSLATE标量函数的第二个和第三个参数必须以正确格式的字符结尾。

是否总有办法解决这个问题?

Is there anyway to get around this?

编辑:@Paul Vernon的解决方案似乎正在起作用:

@Paul Vernon's solution seems to be working:

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


推荐答案

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

Eg

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

返回

1
---------
ABCabc123

BTW请注意,此页面上列出了允许的正则表达式模式正则表达式控制字符

BTW Note that the allowed regular expression patterns are listed on this page Regular expression control characters

在集合之外,必须在后面加上反斜杠以将其视为文字

Outside of a set, the following must be preceded with a backslash to be treated as a literal


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

在一组中,必须在后面加上反斜杠以将其视为文字

Inside a set, the follow must be preceded with a backslash to be treated as a literal


必须被引用为文字的字符是 [] \
可能需要被引用,取决于上下文是-&

这篇关于db2除去所有非字母数字,包括不可打印的和特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆