Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In SQL, how to concatenate string with integer data type column

I am trying to add string "Rs" with amount column. The data type of amount is integer. But Sql is not allowing me to concatenate string with int data type column.Image 1

Image 2

like image 878
Saqib Avatar asked Jul 23 '17 15:07

Saqib


People also ask

Can we concatenate 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 you concatenate a string and an integer?

To concatenate a string to an int value, use the concatenation operator. Here is our int. int val = 3; Now, to concatenate a string, you need to declare a string and use the + operator.

How do I concatenate text and numbers in SQL?

The most obvious (and possibly the best) way to concatenate a string and a number is to use the CONCAT() function. This allows you to provide the string and the number as two separate arguments. SQL Server will then concatenate them, and your concatenation is complete.

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.


1 Answers

You need to explicitly convert the int to varchar before concatenating with any string

select 'RS'+cast(total_amount as varchar(100)),*
from yourtable

If you are sql server 2012+ then use CONCAT which does implicit conversion

select Concat('RS',total_amount),*
from yourtable
like image 171
Pரதீப் Avatar answered Oct 05 '22 23:10

Pரதீப்