tags:

views:

1564

answers:

4

In oracle 10g, how do you convert SYS_GUID() to varchar? I am trying something like:

select USER_GUID from user where email = '[email protected]'

Which returns the RAW byte[]. Is it possible to use a function to convert the RAW to VARCHAR2 in the SQL statement?

+2  A: 

Use RAWTOHEX(USER_GUID).

jon077
+2  A: 

Don't forget to use HEXTORAW(varchar2) when comparing this value to the RAW columns.

There is no implicit convesion from VARCHAR2 to RAW. That means that this clause:

WHERE raw_column = :varchar_value

will be impicitly converted into:

WHERE RAWTOHEX(raw_column) = :varchar_value

, thus making indices on raw_column unusable.

Use:

WHERE raw_column = HEXTORAW(:varchar_value)

instead.

Quassnoi
A: 

Please don't mod-1 if I'm wrong. I'm going from memory so this a disclaimer to verify.

TO_CHAR is actually different between SQL and PL/SQL.

In SQL TO_CHAR does not take a raw as you have found out.

In PL/SQL To_CHAR will take a raw value.

So if you're in a procedure anyways, sometimes it easier to use a variable, but if you're just using SQL, go with the other answers here.

PL/SQL's TO_CHAR() is in fact SYS.STANDARD.TO_CHAR(), which you can use in SQL too: SELECT SYS.STANDARD.TO_CHAR(SYS_GUID()) FROM dual. Of course this implies SQL/PLSQL context switch and appropriate performance impact.
Quassnoi
OMG, that's so cool. I never contextualized it like that. mod+1.1
+1  A: 
select RAWTOHEX(USER_GUID) from user where email = '[email protected]'
geekzspot