Open In App

RAWTOHEX Function in PL/SQL

Last Updated : 09 Jun, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

The objective is to use the function RAWTOHEX in PL/SQL code.

Purpose :
This inbuilt function is beneficial for the conversion of a raw value into a character value in its hexadecimal format.

Syntax :

RAWTOHEX(x) 

where x – raw value to be converted to hexadecimal value and hexadecimal equivalent in the form of a string value is returned.

Here may be a simple procedure to demonstrate the usage of the function –

Example-1: Storing the hexadecimal converted value in a variable ‘random’.

CREATE PROCEDURE TEST_RAW2HEX
IS
   random varchar2(20);
BEGIN
   SELECT RAWTOHEX('JAVA') INTO random FROM dual;
   dbms_output.put_line('random = ' || random);
EXCEPTION
WHEN OTHERS
THEN
   dbms_output.put_line(Exception occurred.)
END TEST_RAW2HEX;

When the procedure is created, and it is run using the SQL script –

BEGIN
    TEST_RAW2HEX;
END;

We get the output as –

random = 5859 

This function accepts argument of any data type other than LONG, LONG RAW, CLOB, BLOB, or BFILE.

Here we see another example of a procedure for the function demonstration.

Example-2: Storing the hexadecimal converted values in variables ‘random_1’ & ‘random_2’.

CREATE PROCEDURE TEST1_RAW2HEX
IS
random_1 varchar2(12);
random_2 varchar2(12);
BEGIN
   SELECT RAWTOHEX('JAVA') INTO random_1 FROM dual;
   SELECT RAWTOHEX('CPP')  INTO random_2 FROM dual;

   dbms_output.put_line('random_1 = ' || random_1);
   dbms_output.put_line('random_2 = ' || random_2);
END TEST1_RAW2HEX;

When the procedure is created, and it is run using the SQL script –

BEGIN
    TEST1_RAW2HEX;
END;

We get the output as –

random_1 = 4A415641
random_2 = 435050 

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads