Posts Tagged ‘database’

Illegal Mix of Collations using CONCAT

Friday, September 4th, 2009

While using MySQL5, I came across this error:

Illegal mix of collations (utf8_general_ci,COERCIBLE) and (latin1_swedish_ci,IMPLICIT) for operation ‘concat’

When I took a look at the query statement, I realized that I was doing a CONCAT with an IF statement being the first part.

EG:
CONCAT(If(1=1,'true','false),' : ',columnb) as variable

This post by Vahid Ghafarpour on Illegal Mix of Collations helped me out when I tried to change the character sets of the database, but I realized that it might be a server default causing this.
Since I didn’t want to change the statement, I changed the query. (I suppose it’s bad practice. :( )

Based on this post at Experts Exchange on the Illegal Mix of Collations, I added COLLATE latin1_swedish_ci after the string. Now, THIS didn’t work:

CONCAT(If(1=1,'true','false),' : ',columnb) as variable COLLATE latin1_swedish_ci
You’ll see why it didn’t work in a second.
CONCAT(If(1=1,'true','false),' : ',columnb) COLLATE latin1_swedish_ci as variable
Didn’t work as well.

What worked is below:
CONCAT(If(1=1,'true','false) COLLATE latin1_swedish_ci ,' : ',columnb) as variable
This is because it’s the IF statement that left its type ambiguous.

Stay cool, coders!