In tracking down a bug today, I needed to put "control" characters into an Oracle SQL statement. Since it was difficult to find using available docs and search engines, I decided to put it here to help out others who may be looking for the same thing. I do not claim this is the best or easiest way of accomplishing it. It just works, and that's what I needed.
So, here is how to escape control characters (or presumably any unicode character) within a string in Oracle SQL or PL/SQL:
UNISTR('\0008')
This will give you the backspace (ASCII 08) character.
So, the UNISTR function encodes backslash ("\") followed by four (hex) digits as a single unicode character. (actually it doesn't need to be 4 hex digits. It will stop at the first non-hex digit. But, it will still "eat" 4 characters from the string.)
(Note: this is for XXXX2 column types like VARCHAR2 that support unicode. I don't know if/how this works on non-unicode types. Also, this is for Oracle 10g.)
UPDATE: Shortly after writing this, I discovered ASCIISTR, which is identical in syntax to and probably preferable in the case of control characters (as opposed to real unicode characters like accents, symbols, etc.) ASCIISTR is likely compatible with earlier versions of Oracle as well.
Remember Me
Page rendered at Monday, September 08, 2008 7:47:37 AM (Pacific Standard Time, UTC-08:00)
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.