sql >> Database >  >> RDS >> PostgreSQL

MySQL's HEX() en UNHEX() equivalent in Postgres?

create function hex(text) returns text language sql immutable strict as $$
  select encode($1::bytea, 'hex')
$$;

create function hex(bigint) returns text language sql immutable strict as $$
  select to_hex($1)
$$;

create function unhex(text) returns text language sql immutable strict as $$
  select encode(decode($1, 'hex'), 'escape')
$$;


select hex('abc'), hex(123), unhex(hex('PostgreSQL'));

Resultaat:

╔════════╤═════╤════════════╗
║  hex   │ hex │   unhex    ║
╠════════╪═════╪════════════╣
║ 616263 │ 7b  │ PostgreSQL ║
╚════════╧═════╧════════════╝

Het is PostgreSQL:alles mogelijk :)



  1. SELECT INTO OUTFILE gebruiken in MySQL

  2. INSTR() Equivalent in SQL Server

  3. Topantwoorden op 5 brandende vragen over de COALESCE-functie in SQL Server

  4. Web2py – Migreren van SQLite naar MySQL