tags:

views:

180

answers:

2

Hai all,

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.

thank you very much.

+2  A: 

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.

Michal Pravda
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
A: 

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
Rene
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