Strange SUM and CONCAT behavior in MySql

If I want to make the sum of a specific numeric column in MySQL, I do

SELECT SUM(MyColumn) FROM MyTable WHERE 1; 

This returns, for example, number 100.

But I would like to add some text to the sum value, so I do

 SELECT CONCAT('Sum is: ',SUM(MyColumn)) FROM MyTable WHERE 1; 

but instead of getting Sum is: 100 I get something like 546573743a20343030 .

Is this a bug or function? What am I doing wrong?

UPDATE

 SELECT CONCAT('Sum is: ',CAST(SUM(MyColumn) AS varchar(20))) FROM MyTable WHERE 1; 

Casting in varchar does not work: getting SQL syntax error.

+7
mysql aggregate-functions
source share
1 answer

Like FreshPrinceOfSO suggested in the comments below my question, the MySQL server does not handle clicks on varchar .

So even if the request

 SELECT CONCAT('Sum is: ',CAST(SUM(MyColumn) AS varchar(20))) FROM MyTable WHERE 1; 

leads to a syntax error, and an excellent disconnect to char works fine:

 SELECT CONCAT('Sum is: ',CAST(SUM(MyColumn) AS char(20))) FROM MyTable WHERE 1; 
+6
source share

All Articles