删除特殊字符chr(191)和其他SQL

时间:2016-01-11 06:28:16

标签: sql oracle unix special-characters

unix脚本和Oracle命令的新手,所以很容易......

我有一个填充了错误字符的字段,特别是chr(191)'倒置了?签署'..

我有这个示例代码

UPDATE rc.rc_descripton
SET desc_description = 
                        (
                         CASE 
                            WHEN desc_description LIKE '%[^a-zA-Z0-9]%' 
                                  THEN Replace(REPLACE( desc_description, SUBSTRING( desc_description, PATINDEX('%[~,@,#,$,%,&,*,^,&,%,*,(,)]%', desc_description), 1 ),''),'-',' ')
                            ELSE desc_description
                          END
                         )

虽然我原来的UPDATE命令是: -

UPDATE rc.rc_description
SET    desc_description  = REPLACE(desc_description, CHR(191), ' ')
WHERE  desc_description LIKE '%' || CHR(191) || '%'

我想将上面的代码合并到我上面发布的主要示例代码中。

我确实找到了之前使用过的unix脚本:

# Replace offending characters with the intending/valid characters 

sed s/–/-/g $1 | sed s/’/\'/g | sed s/‘/\'/g | sed s//\'/g | sed s//\'/g | sed s//\"/g | sed s//\"/g | sed s/“/\"/g | sed s/”/\"/g | sed s/—/-/g | sed s//-/g | sed s/…/.../g | sed s/‑/-/g | sed 's/½/1\/2/g' | sed 's/¼/1\/4/g' | sed 's/¾/3\/4/g' | sed 's/· //g' | sed s/°/./g | sed s/•/*/g | sed s/ //g > $1.out

但是我无法将unix代码翻译成Oracle更新命令..

请告知正确的UPDATE命令,以满足上述所有特殊字符集,并考虑CHR(191)...

此致 A·B

2 个答案:

答案 0 :(得分:0)

我得到以下声明为我工作: -

 select desc_description,
  length(dbms_lob.substr(desc_description, 3500, 1)) len,
  lengthb(dbms_lob.substr(desc_description, 3500, 1)) lenb,

  convert(replace(translate(dbms_lob.substr(desc_description, 3500, 1), ' abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-=`~!@#$%^&*()_+,./<>?;'':"[]\{}|',' '), ' ', ''),'us7ascii') bad_char_replaced,

  rawtohex(convert(replace(translate(dbms_lob.substr(desc_description, 3500, 1), ' abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-=`~!@#$%^&*()_+,./<>?;'':"[]\{}|',' '), ' ', ''),'us7ascii')) bad_char_replaced_hex ,
  -------
  convert(dbms_lob.substr(desc_description, 3500, 1),'us7ascii') converted
  -------
  from rc.rc_description

  WHERE  desc_description LIKE '%' || CHR(191) || '%' and
  desc_cras_number = '20140096849';

之前的文字是: 车辆B柱已经......

After文本是: vehiclei ?? s B柱有......

我可以获取上述查询以删除这些???撇号还是双引号?或者,如果这会拧紧数据,那么jst使用空格。

答案 1 :(得分:-2)

尝试使用您的字符串替换输入,正则表达式是字母数字,您可以轻松地操作它 T-SQL语法

DECLARE @input AS NVARCHAR(MAX) = '124567890qwertyuioopasdfghjklzx cvbnm,;#$%^&*(!)$^'
DECLARE @output AS NVARCHAR(MAX) = @input
DECLARE @RegexString AS NVARCHAR(max) = '[^a-zA-Z0-9 ]'

WHILE PATINDEX('%'+@RegexString+'%',@output)>0
BEGIN
    SET     @output=  STUFF(@output,PATINDEX('%'+@RegexString+'%',@output),1,'')
    PRINT   @output
END

SELECT @input , @output