SQL Server uses "+" operator for concatenation of strings.
I.e. if operands are numeric, the sum is being calculated, if operands are strings, we have concatenation:
If operands are of different data types, Cèñòåìà óïðàâëåíèÿ ðåëÿöèîííûìè áàçàìè äàííûõ (ÑÓÁÄ), ðàçðàáîòàííàÿ êîðïîðàöèåé Microsoft. SQL(Structured Query Language) is a database computer language designed for the retrieval and management of data in relational database management systems (RDBMS), database schema creation and modification, and database object access control management.SQL Server performs implicit type conversion. When executing the following query
Error converting data type varchar to real.
There is type priority when implicit conversion is being performed, and this priority causes the server to transform the character string ' Gb' to the data type of the hd column (real data type).
Needless to say that the explicit type conversion solves the problem:
CONCAT function is in SQL Server since version 2012. This function concatenates its arguments been implicitly converting them to character string data type. With aid of this function the above query can be rewritten as follows.
Another useful feature of CONCAT function is that NULL values are being implicitly converted to empty string - ''. Ordinary concatenation with NULL gives NULL. Here is example
It should be noted that CONCAT function has variable number of parameters, but not less than two.
MySQL has CONCAT function also, even two functions. The first one - CONCAT - return NULL if anyone of parameters is NULL. The second one - CONCAT_WS - ignores the NULL valued argument. Moreover, this function include separator as the first parameter. The separator is being placed between concatenated parameters.
Suggested exercises: 119