How to find ASCII of every character in string using DB2 Function?

♀尐吖头ヾ 提交于 2021-02-08 08:52:20

问题


I have written a function in DB2 - that is calculating ASCII of records in a particular column. I want to some help as I want to check the ASCII of every single character in string return yes if the ASCII of that record is greater than 127.

BEGIN
ATOMIC DECLARE POS,
INT;

IF INSTR IS NULL THEN RETURN NULL;

END IF;

SET
(
    POS,
    LEN
)=(
    1,
    LENGTH(INSTR)
);


WHILE POS <= LEN DO IF ASCII( SUBSTR( INSTR, POS, 1 ))> 128 THEN RETURN 'Y';

END IF;

SET
POS = POS + 1;

END WHILE;


RETURN 'N';

回答1:


Why to calculate ascii of every character in that column, if the goal is just to get such rows?

SELECT STR
FROM 
(
VALUES 
  'Hello, world'
, 'Привет, мир'
) T (STR)
WHERE xmlcast(xmlquery('fn:matches($s, "[^\x00-\x7F]")' passing t.str as "s") as int) = 1;

The fn:matches function uses regular expressions.
The [^\x00-\x7F] regular expression means "a character with hex value not in the 0x00 - 0x7F interval". If a value of passed t.str contains such a character, the function returns 1 and 0 otherwise.




回答2:


A simple way to check if a UTF-8 value in DB2 only contains "plain ASCII" is to compare it's BYTE length with it's STRINGUNITS32 length. E.g.

SELECT
    S
,   LENGTHB(S) AS BYTES
,   LENGTH4(S) AS CHARACTERS 
,   LENGTHB(S) = LENGTH4(S) PLAIN_ASCII
FROM 
    TABLE(VALUES ('123!"$'),('¹²³€½¾')) T(S)

returns

S     |BYTES|CHARACTERS|PLAIN_ASCII
------|-----|----------|-----------
123!"$|    6|         6|true       
¹²³€½¾|   13|         6|false      

The above assumes your database is Unicode, and the columns are VARCHAR. If they are VARGRAPHIC (i.e. UTF-16), then you would need to cast them to VARCHAR in your comparision



来源:https://stackoverflow.com/questions/59473002/how-to-find-ascii-of-every-character-in-string-using-db2-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!