db2 去除全角字符

最近在做报表,需求中要求把一字段剔除全角字符然后做相似检查,再网上搜了好多关于db2判断是否全角字符的例子,然后写了下面一函数,还好使,不知道有没有bug存在,贴出来,望大牛指正........


CREATE FUNCTION f_getIdNum(inStr VARCHAR(255))
RETURNS VARCHAR(255)
LANGUAGE SQL


BEGIN ATOMIC
DECLARE outStr VARCHAR(255) DEFAULT '';
DECLARE k INT DEFAULT 1;
----判断是否存在全角字符,存在则进行剔除
if(length(inStr) != length(inStr, CODEUNITS32)) then
WHILE(k<=length(inStr)) do
if(ascii(substr(inStr,k,1))< 128) then
set outStr = outStr || substr(inStr,k,1);
end if;
set k = k+1;
END WHILE;
ELSE
SET outStr = outStr || inStr;
end if;

RETURN outStr;
END;

你可能感兴趣的:(db2 去除全角字符)