Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get a syntax error when using CAST in MySQL?

I am using MySQL workbench v5.2.44 CE. I am running it against a local MySQL 5.5 install.

I am trying to use the CAST function, but keep getting the following error:

syntax error, unexpected INT_SYM

It doesn't matter what the source and target date types are. The only time it doesn't give me an error is when the target datatype is DECIMAL. Here is an example:

SELECT CAST(IFNULL(comboCount, 1) * COUNT(partID) AS INT) INTO comboCount
FROM productOption

I have tried everything, but nothing seems to work.

like image 868
Silent User Avatar asked Jan 25 '13 22:01

Silent User


People also ask

Does CAST work in MySQL?

MySQL CAST() FunctionThe CAST() function converts a value (of any type) into the specified datatype.

How do I CAST datatype in MySQL?

The MySQL CAST() function is used for converting a value from one datatype to another specific datatype. The CAST() function accepts two parameters which are the value to be converted and the datatype to which the value needs to be converted.

What is syntax error in MySQL?

The MySQL 1064 error is a syntax error. This means the reason there's a problem is because MySQL doesn't understand what you're asking it to do. However, there are many different situations that can lead to this type of miscommunication between you and your database.


1 Answers

Try to do the math outside:

SELECT CAST(IFNULL(comboCount, 1) AS INT) * COUNT(partID) INTO comboCount
FROM productOption

If that doesn't work, try to CAST as UNSIGNED; not INT.

like image 80
Kermit Avatar answered Oct 25 '22 17:10

Kermit