Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to concat datatypes like integers(integer with integer) & varchar(varchar with varchar) in mysql?

Tags:

mysql

How can we concat

  1. integers with integers
  2. varchar with varchar
  3. int with varchar

in MySQL ?

like image 926
Rachel Avatar asked Mar 19 '10 16:03

Rachel


People also ask

Can you concatenate integers in SQL?

In SQL, you can also concatenate numerical data from the table in the same way as we concatenate strings. The CONCAT function can also be used to join numeric values.

How do you concatenate an integer in Python?

If you want to concatenate a string and a number, such as an integer int or a floating point float , convert the number to a string with str() and then use the + operator or += operator.

Can we concat string and integer in SQL?

To concatenate we can use + sign but this works only with String values. So if we have any Integer value/s we have to convert them to String first. We can use Cast or Convert function to convert Integer value to string.

How do I concatenate two columns with different data types in SQL?

Solution. TSQL provides 2 ways to concatenate data, the + sign and the new CONCAT() function. This tip will cover the differences in the two, so you can achieve the expected behavior in your code. The way most us are used to concatenating data together is using the + sign.


2 Answers

Use CONCAT

http://dev.mysql.com/doc/refman/5.1/en/string-functions.html#function_concat

SELECT CONCAT(1, 2);
-- "12"

SELECT CONCAT('foo', 'bar');
-- "foobar"

SELECT CONCAT(1, 'bar');
-- "1bar"
like image 74
nickf Avatar answered Oct 13 '22 09:10

nickf


if the arguments include any binary strings, the result is a binary string. A numeric argument is converted to its equivalent binary string form; if you want to avoid that, you can use an explicit type cast, as in this example:

SELECT CONCAT(CAST(int_col AS CHAR), char_col);

5.1 Documentations

like image 38
MrM Avatar answered Oct 13 '22 10:10

MrM