Oracle
 sql >> Base de Dados >  >> RDS >> Oracle

Descubra se uma string contém apenas caracteres ASCII


Você pode usar TRANSLATE para fazer isso. Basicamente, translate todos os caracteres imprimíveis ASCII (não há muitos deles) e veja o que resta.

Aqui está uma consulta que faz isso:
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:;<=>[email protected][\]^_`abcdefghijklmnopqrstuvwxyz{|}~]', 
       chr(0)) is null then 'Yes' else 'No' END is_ascii
FROM input;