String concatenation operator is used to concatenate strings.
Addition sign (+) is used to concatenate strings. It is also called string
cancatenation operator.
Syntax of using string concatenation operator :
SELECT ColumnOfTypeString1
+ ColumnOfTypeString2…. + ColumnOfTypeStringN
FROM table
Examples of using string concatenation operator in SELECT
clause :
Example 1 : Using string cancatenation operator + to add 2
strings
SELECT ‘Hello’ + ‘world’
Output
Hello world
Strings 'Hello' and ' world' are concatenated using + string concatenation operator.
Example 2 : Using string cancatenation operator + in SELCT
caluse to concatenate strings and table fields.
SELECT 'Unit price of ' + LOWER(ProductName) + ' is $' + CONVERT(VARCHAR,UnitPrice) AS 'Product unit price'
FROM Products
Output
Product unit price
Unit price of
chai is $18.00
Unit price of
chang is $19.00
Unit price of
aniseed syrup is $10.00
Unit price of
chef anton's cajun seasoning is $22.00
Unit price of chef
anton's gumbo mix is $21.35
Unit price of
grandma's boysenberry spread is $25.00
As an above
example string ‘Unit price of’, ProductName, ‘is $’ and UnitPrice as string are
concatenated using + operator. UnitPrice needs to convert from money datatype
to varchar, to add it as string otherwise it will give an error.