postgresqlhexto-char

to_char function hexadecimal formats error in Postgresql


I want to use to_char function in Postgresql but take an error when execute the script.

Oracle version is ok;

to_char('7374961057827412212','XXXXXXXXXXXXXXXXXXXX') 

result : 66592002042458F4

But I could not find Postgresql version and take an error like this;

ERROR:  function to_char(text, unknown) does not exist

Solution

  • If you look at the table of formatting codes for numbers, you will see that X is not supported, and indeed there is no way to get hexadecimal output with to_char.

    But you can use to_hex:

    SELECT to_hex(7374961057827412212);
    
          to_hex      
    ══════════════════
     66592002042458f4
    (1 row)
    

    The error message you see is because you entered the first argument in single quotes, so it is a string (data type text), but there is no to_char function to format strings as strings (they are already strings).