Find out if a string contains only ASCII characters

后端 未结 3 1433
傲寒
傲寒 2020-12-06 15:07

I need to know whether a string contains only ASCII characters. So far I use this REGEX:

DECLARE
    str VARCHAR2(100) := \'xyz\';
BEGIN
    IF REGEXP_LIKE(s         


        
3条回答
  •  醉梦人生
    2020-12-06 15:39

    You can use TRANSLATE to do this. Basically, translate away all the ASCII printable characters (there aren't that many of them) and see what you have left.

    Here is a query that does it:

    WITH input ( p_string_to_test) AS ( 
    SELECT 'This this string' FROM DUAL UNION ALL
    SELECT 'Test this ' || CHR(7) || ' string too!' FROM DUAL UNION ALL
    SELECT 'xxx' FROM DUAL)
    SELECT p_string_to_test, 
           case when translate(p_string_to_test, 
           chr(0) || q'[ !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~]', 
           chr(0)) is null then 'Yes' else 'No' END is_ascii
    FROM input;
    
    +-------------------------+----------+
    |    P_STRING_TO_TEST     | IS_ASCII |
    +-------------------------+----------+
    | This this string        | Yes      |
    | Test this  string too!  | No       |
    | xxx                     | Yes      |
    +-------------------------+----------+
    

提交回复
热议问题