5

Why doesn't the CAST work in this MySQL query?

SELECT  MAX(Signups) AS Max, 
        MIN(Signups) AS Min,
        CAST(ROUND(AVG(Signups),2) AS VARCHAR(3)) AS Avg
FROM
(
    SELECT COUNT(1) AS Signups,
    DATE_FORMAT(JoinDate, "%Y-%m-%d") AS Date
    FROM users
    GROUP BY Date
) z 

Why am I getting this error?

#1064 - You have an error in your SQL syntax; check the manual that corresponds 
to your MySQL server version for the right syntax to use near 
'VARCHAR(3)) AS Avg FROM ( ' at line 2
0

2 Answers 2

10

This may be due to MySQL bug #34564: CAST does not accept varchar type.

Try casting to a different type, like this:

CAST(ROUND(AVG(Signups),2) AS CHAR(3)) AS Avg
Sign up to request clarification or add additional context in comments.

3 Comments

Note that this was the first Google result for "mysql cast to varchar", and I found it within seconds.
I did not know varchar was the issue hence I did not use your search term to resolve the issue.
@Nai: But it's what you're doing!
1

Changing VARCHAR to CHAR solves the problem

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.