16

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

select USER_GUID from user where email = 'user@example.com'

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

5 Answers 5

23

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.

5

Use RAWTOHEX(USER_GUID).

4
select RAWTOHEX(USER_GUID) from user where email = 'user@example.com'
1

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.

2
  • 2
    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
    Mar 3, 2009 at 0:06
  • 1
    OMG, that's so cool. I never contextualized it like that. mod+1.1
    – Mark Brady
    Mar 3, 2009 at 18:11
0

select CAST (USER_GUID AS VARCHAR2(100)) from user where email = 'user@example.com'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.