stramin stramin - 6 months ago 7
SQL Question

MySQL - Is hexadecimal

How could I check if a varchar code is hexadecimal in MySQL?

Something like a ISHEX() function:


  • ISHEX('QWERT') // false

  • ISHEX('12345') // true

  • ISHEX('ABCDE') // true

  • ISHEX('i+5=G') // false


Answer

How about using the REGEXP operator?

SELECT 'QWERT' REGEXP '^[0-9ABCDEF]+$'; -- 0
SELECT '12345' REGEXP '^[0-9ABCDEF]+$'; -- 1
SELECT 'ABCDE' REGEXP '^[0-9ABCDEF]+$'; -- 1
SELECT 'i+5=G' REGEXP '^[0-9ABCDEF]+$'; -- 0
Comments