CAST()

Overview

Takes a value of one type and converts it to the value of another of type. See CONVERT.

USAGE

DETAILS

SYNONYMS

SCHEMA

PARAMETERS

SKYSQL

PRIVILEGES

EXAMPLES

CREATE TABLE cast_example (
  example VARCHAR(32)
);
INSERT INTO cast_example VALUES
  ('apple '), ('Apricot  '), ('berry'),
  ('Cheese   '), ('Dairy  '), ('eggs ');

Notice that casing a VARCHAR to a CHAR does not auto-remove the trailing spaces (since the space removal only happens when a value is retrieved from a field):

SELECT example, LENGTH(example), LENGTH(CAST(example AS CHAR))
FROM cast_example ORDER BY example;
+-----------+-----------------+-------------------------------+
| example   | LENGTH(example) | LENGTH(CAST(example AS CHAR)) |
+-----------+-----------------+-------------------------------+
| apple     |               6 |                             6 |
| Apricot   |               9 |                             9 |
| berry     |               5 |                             5 |
| Cheese    |               9 |                             9 |
| Dairy     |               7 |                             7 |
| eggs      |               5 |                             5 |
+-----------+-----------------+-------------------------------+

You can make case significant in a sort by casting to a BINARY type:

SELECT * FROM cast_example ORDER BY CAST(example AS BINARY);
+-----------+
| example   |
+-----------+
| Apricot   |
| Cheese    |
| Dairy     |
| apple     |
| berry     |
| eggs      |
+-----------+
CREATE TABLE timezone (t TIMESTAMP);
INSERT INTO timezone VALUES ('2020-10-13T23:23:28');
SELECT CAST(t AS DATETIME) FROM timezone;
+---------------------+
| CAST(t AS DATETIME) |
+---------------------+
| 2020-10-13 23:23:28 |
+---------------------+

ERROR HANDLING

FEATURE INTERACTION

RESPONSES

DIAGNOSIS

ISO 9075:2016

CHANGE HISTORY

Release Series

History

10.6 Enterprise

  • Present starting in MariaDB Enterprise Server 10.6.4-1.

10.6 Community

  • Present starting in MariaDB Community Server 10.6.0.

10.5 Enterprise

  • Present starting in MariaDB Enterprise Server 10.5.3-1.

10.5 Community

  • Present starting in MariaDB Community Server 10.5.0.

10.4 Enterprise

  • Present starting in MariaDB Enterprise Server 10.4.6-1.

10.4 Community

  • Present starting in MariaDB Community Server 10.4.0.

10.3 Enterprise

  • Present starting in MariaDB Enterprise Server 10.3.16-1.

10.3 Community

  • Present starting in MariaDB Community Server 10.3.0.

10.2 Enterprise

  • Present starting in MariaDB Enterprise Server 10.2.25-1.

10.2 Community

  • Present starting in MariaDB Community Server 10.2.0.

Release Series

History

10.6 Enterprise

  • Present starting in MariaDB Enterprise Server 10.6.4-1.

10.5 Enterprise

  • Present starting in MariaDB Enterprise Server 10.5.3-1.

10.4 Enterprise

  • Present starting in MariaDB Enterprise Server 10.4.6-1.

EXTERNAL REFERENCES