Updated on 2026-11-06 GMT+08:00

encode

The encode function is used to encode str using the encoding method specified by charset.

Syntax

encode(string , string )

Parameters

Parameter

Mandatory

Type

Description

str

Yes

STRING

String to be encoded. Type STRING. If the input is of type BIGINT, DECIMAL, DOUBLE, or DATETIME, it is implicitly converted to STRING type before evaluation.

charset

Yes

STRING

The encoding format. Valid values are: UTF-8, UTF-16, UTF-16LE, UTF-16BE, ISO-8859-1, US-ASCII.

Return Values

Returns a value of type BINARY.

If str or charset is NULL, NULL is returned.

Example Code

  • Encode the string abc using UTF-8 format. The command example is as follows.
    Returns YWJj.
    select encode("abc", "UTF-8");
  • Any input parameter is NULL. The command example is as follows.

    Returns NULL.

    select encode("abc", null);