I am working on SSRS Report.
I have one question, what is the difference between +
and &
in ssrs-expression?
Please share your answer with short example. Thanks.
I am working on SSRS Report.
I have one question, what is the difference between +
and &
in ssrs-expression?
Please share your answer with short example. Thanks.
From SSRS operators description in Visual Studio:
+
Add two numbers. Also used to concatenate two strings.
Example:
=Fields!NumberCarsOwned.Value + 2
/* if NumberCarsOwned=10, result: 10 + 2 = 12 */
=Fields!String1.Value + Fields!String2.Value
/* if String1='curious' and String2 ='guy', result: 'curiousguy' */
="4" + 5
/* result: 9, implicit conversion of the first expression */
="a" + 5
/* error: incorrect input string format */
="a" + CStr(5)
/* result: a5 */
="a" + Str(5)
/* result: a 5, space between 'a' and '5' */
&
Generates a string concatenation of two expressions.
Example:
=Fields!FirstName.Value & Fields!LastName.Value
/* if FirstName='curious' and LastName='guy', result: 'curiousguy' */
=4 & 5
/* it's concatenation anyway, result: 45 */
=CInt(4) & CInt(5)
/* even explicit cast to integer it's concatenation anyway, result: 45 */
Another useful operator is And
(logical/bitwise AND), which may help to solve some tasks.
Example:
=4 And 5
/* result: 4, since 4 (100 binary) And 5 (101 binary) = 4 (100 binary) */
="4" And "5"
/* result: 4 */
="a" And "b"
/* error: incorrect input string format */
Actually its depends on how you are going to use them.
1.) For Concatenation purpose
According to MSDN:
&
and +
for concatenation will result to the same behaviour or output. It will only concatenates two strings.Example:
= 1 & 2
Output: 12
= "1" + "2"
Output: 12
2.) For Arithmetic purpose
+
for arithmetic purpose will add the two numbers togetherExample:
= 1 + 2 //(using two numbers)
Output: 3
= 1 + "2" //(using a number and a number with quotes)
Output: 3
------------------------------------------------------------------------
Exceptions
Now there are exceptions that an integer and a string is mixed up or with other types
Example:
= 1 + "two"
Output: #Error
-> this is because they are incompatible with each other.
Example:
= CSTR(1) + "two"
Output: 1two
There are other more conversion functions which you can use depending on your needs.