I'm trying to cast a string data to a TINYINT data but I'm getting below error.

SELECT CAST('100' as TINYINT);

> 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'TINYINT)' at line 1

Casting to INT works fine.

SELECT CAST('100' as INT);

Any body can help me?

有帮助吗?

解决方案

The syntax for CAST() is CAST(expr AS type [ARRAY]).
You got the syntax right but you were unable to do the cast because TINYINT is not among the list of permitted values for 'type' in MySQL.
Casting to INT works in MariaDB but it doesn't in MySQL.
MySQL only allow any one of the following to be used as value for 'type':
- BINARY.
- CHAR
- DATE
- DATETIME
- DECIMAL
- DOUBLE
- FLOAT
- JSON
- NCHAR
- REAL
- SIGNED
- TIME
- UNSIGNED

See https://dev.mysql.com/doc/refman/8.0/en/cast-functions.html#function_cast

许可以下: CC-BY-SA归因
不隶属于 dba.stackexchange
scroll top