decode (character set) function

Applies to: check marked yes Databricks SQL check marked yes Databricks Runtime

Translates binary expr to a string using the character set encoding charSet.

Syntax

decode(expr, charSet)

Arguments

  • expr: A BINARY expression encoded in charset.

  • charSet: A STRING expression.

Returns

A STRING.

If charSet does not match the encoding the result is undefined. charSet must be one of (case insensitive):

  • ‘US-ASCII’

  • ‘ISO-8859-1’

  • ‘UTF-8’

  • ‘UTF-16BE’

  • ‘UTF-16LE’

  • ‘UTF-16’

Examples

-- Wrap encode in hex to normalize UI dependent BINARY output.
> SELECT hex(encode('Spark SQL', 'UTF-16'));
 FEFF0053007000610072006B002000530051004C

> SELECT hex(encode('Spark SQL', 'US-ASCII'));
537061726B2053514C

> SELECT decode(X'FEFF0053007000610072006B002000530051004C', 'UTF-16')
 Spark SQL