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
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With