sql >> Base de Datos >  >> RDS >> Oracle

Averigüe si una cadena contiene solo caracteres ASCII

Puedes usar TRANSLATE para hacer esto. Básicamente, translate elimine todos los caracteres imprimibles ASCII (no hay tantos) y vea lo que le queda.

Aquí hay una consulta que lo hace:

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;