Question

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?

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange
scroll top