1

How can I use Presto sql and check if value of a column 'cola' is Hexadecimal? My goal is to resolve the issue of 'Not a valid base-16 number' when data is messy. I've tried below but not working.

regexp_like(cola, '^[^g-zG-Z]*$')
translate(upper(cola), '0123456789ABCDEF', '.') = '.'

Curious how do you check that in Presto? Thanks!

1 Answer 1

2

You can wrap the translation (via from_base) into try which will return null if the translation fails:

-- sample data
WITH dataset(hex_string) as (
    values ('0123456789ABCDEF'),
           ('0G')
)

-- query
select try(from_base(hex_string, 16)) IS NOT NULL is_hex
from dataset;

Output:

is_hex
true
false
Sign up to request clarification or add additional context in comments.

1 Comment

Sure and just upvoted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.