Find out if a string contains only ASCII characters
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 ALLSELECT 'Test this ' || CHR(7) || ' string too!' FROM DUAL UNION ALLSELECT '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_asciiFROM input;
+-------------------------+----------+| P_STRING_TO_TEST | IS_ASCII |+-------------------------+----------+| This this string | Yes || Test this string too! | No || xxx | Yes |+-------------------------+----------+
ASCII
function with upper limit of 127
may be used :
declare str nvarchar2(100) := '\xyz~*-=)(/&%+$#£>|"éß'; a nvarchar2(1); b number := 0;begin for i in 1..length(str) loop a := substrc(str,i,1); b := greatest(ascii(a),b); end loop; if b < 128 then dbms_output.put_line('String is composed of Pure ASCII characters'); else dbms_output.put_line('String has non-ASCII characters'); end if; end;