I have noticed that, in his latest book, Tom Kyte used the SQL function DUMP quite frequently to expose the internal representation and the size of a specific expression. To be honest, I do not use DUMP very often, but I do find it useful in certain situations.
From the Oracle documentation:
DUMP(expr[, return_fmt
[, start_position [, length ] ]
]
)
DUMP returns a VARCHAR2 value containing the datatype code, length in bytes, and internal representation of expr. The returned result is always in the database character set.
For example:
HR@XE> select dump('hello') from dual;
DUMP('HELLO')
---------------------------------
Typ=96 Len=5: 104,101,108,108,111
The result tells me that the length of ‘hello’ is 5 bytes, its internal representation in decimal is “104,101,108,108,111″, and its type is 96 which means a character datatype. Oracle docs gives a complete list of type codes and descriptions.
Decimal notation is the default. You can format the return value in octal, hexadecimal and as single characters. For example, in octal:
HR@XE> select dump('hello',8) from dual;
DUMP('HELLO',8)
---------------------------------
Typ=96 Len=5: 150,145,154,154,157
in hexadecimal:
HR@XE> c/8/16
1* select dump('hello',16) from dual
HR@XE> /
DUMP('HELLO',16)
----------------------------
Typ=96 Len=5: 68,65,6c,6c,6f
and as single characters:
HR@XE> c/16/17
1* select dump('hello',17) from dual
HR@XE> /
DUMP('HELLO',17)
-----------------------
Typ=96 Len=5: h,e,l,l,o
By default, the return value contains no character set information. To retrieve the character set name, add 1000 to any of the preceding format values. For example, a format of 1017 (instead of 17) returns the result in single characters and provides the character set name as well:
HR@XE> c/17/1017
1* select dump('hello',1017) from dual
HR@XE> /
DUMP('HELLO',1017)
-------------------------------------------------
Typ=96 Len=5 CharacterSet=WE8MSWIN1252: h,e,l,l,o
You can also ask Oracle to return a portion of the internal representation by providing values for start_position and length:
HR@XE> select dump('hello',17,1,3) from dual;
DUMP('HELLO',17,1,3
-------------------
Typ=96 Len=5: h,e,l
Notice that the length is still 5, only the internal representation is “sub-stringed”.
Just curious, have you used the SQL function DUMP in any of your production applications where the logic of the program depended on the result of this function?
Possibly related:
- Cool Undocumented SQL Function: REVERSE
- Did You Know That About PL/SQL Variables?
- Cool SQL function: EXTRACT
- Cool SQL Analytic Function: RATIO_TO_REPORT
- Cool SQL Analytic Function: NTILE
Tagged function, sql | Post a Comment


















Haven’t used it in Production code, but its handy for debugging (looking for trailing spaces or control characters such as a CR in VARCHAR2 fields).
One aspect I like is that DUMP(val) returns the string ‘NULL’ for null values, so DUMP(val_1) = DUMP(val_2) returns TRUE if both values are NULL.
January 12th, 2006, at 1:13 pm #However because DUMP also shows the datatype, the same returns false for, for example, val_1 is the number 10 and val_2 the string ‘10′.
The dump command is very handy, and I used it to perform character set conversions when I had to migrate an Oracle 8 database to an Oracle 9iR2 database.
January 12th, 2006, at 2:36 pm #never in prodcution applications, just use it for some data storage internal test.
January 12th, 2006, at 8:59 pm #I found your posts always be interesting, I’m a Oracle DBA living in China, maybe I can translate some of your articles into Chinese, but I cannnot find any Creative Commons license in your site, so if you don’t mide my translation please email to me. Thanks.
kamusis [at] gmail [dot] com
January 12th, 2006, at 9:06 pm #Cool SQL function: DUMP
å¾Â求了Eddie Awad…
January 12th, 2006, at 10:26 pm #