1

I have a table tbl_1 with fields fl_1 (varchar2 type), contain Hexadecimal format. Now i want validate (query) where is in Hexa format and where is not.

any idea in oracle script (SQL Syntax)?

CREATE TABLE TBL_1 (HEX VARCHAR2(20));
INSERT INTO TBL_1 VALUES('9851010A'); 
INSERT INTO TBL_1 VALUES('9851010B'); 
INSERT INTO TBL_1 VALUES('FIRDI2'); 
INSERT INTO TBL_1 VALUES('FIRDI'); 
commit;

should only value 'FIRDI' AND 'FIRDI2' are false.

3
  • Hexadecimal or what? You won't be able to tell hex from decimal except heuristically.
    – hobbs
    Sep 29, 2009 at 5:34
  • Hai hobbs, I have revision my question with my table condition. thanks
    – Firdi
    Sep 29, 2009 at 5:45
  • 1
    For future table design, I would seriously consider storing hexadecimal values as numbers, as that's what they are. Convert to hex for display or whatever, but don't store them as varchar2.
    – Rob
    Sep 29, 2009 at 15:04

3 Answers 3

3

Just test, whether the field contains only hex characters:

SELECT *
  FROM tbl_1
 WHERE translate(upper(fld_1),
                 '0123456789ABCDEF',
                 NULL) IS NOT NULL;

This query returns rows with non hexadecimal values. You may or may not add trim() to the condition. It depends whether space padding invalidates data in your point of view or not.

2
  • Hai Michal, I modify your script and it works for me: SELECT T.* FROM TBL_1 T WHERE translate(upper(T.HEX),'0123456789ABCDEF', '.') != '..'; thanks for you clue.
    – Firdi
    Sep 29, 2009 at 6:43
  • 1
    That answer seems wrong. If one argument of translate is NULL, the result is also NULL, see docu...
    – wolφi
    Dec 4, 2015 at 12:49
1

I was unable to work the above translate example into a reliable solution.

I instead created a solution using REGEXP_INSTR.

SELECT * FROM tbl_1 WHERE REGEXP_INSTR( UPPER(fld_1), '[^0123456789ABCDEF]' ) > 0;

The above will return rows that include non hex characters.

I also turned this into a is_hex function that either returns 'T' or 'F':

create or replace FUNCTION "IS_HEX" (i_value in VARCHAR2)
RETURN CHAR
IS
    v_result CHAR(1) := 'F';
BEGIN

    IF REGEXP_INSTR( UPPER(i_value), '[^0123456789ABCDEF]') = 0 THEN
        v_result := 'T';
    END IF;

    return v_result;
END;

Allowing you to do the following:

SELECT * FROM tbl_1 WHERE is_hex(fld_1) = 'F';
0

You can use the to_number and to_char functions for hex translations.

SELECT TO_number('0A','XX') from dual
10

SELECT TO_CHAR(10,'XX') from dual
 A

If you have a string that does not represent a hex number you will get an invalid number exception.

SELECT TO_number('RR','XX') from dual

ORA-01722: invalid number
1
  • Hai Rene, thanks for your answering, I have the answer from Michal P's script with small modification for me, thanks a alot for your attention.
    – Firdi
    Sep 29, 2009 at 8:27

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.