What does the SQL CONCAT function do?
Working with databases is constantly associated withreceiving query results. And in some cases this information needs to be displayed on a screen in a certain way or combined with other data. To solve this problem there is a function SQL - CONCAT.
What does the CONCAT function do?
When performing some work with databases,there is a need for connecting strings with additional data or with each other. To solve such a problem there is a function SQL - CONCAT. When you use it, two or more rows will be merged into one. In this case, the string concatenation operation will be correctly performed both with the operation of strings and with numerical variables.
In some SQL databases, CONCAT hasseveral varieties. So, in MySQL it has its own analogue - CONCAT_WS. The difference between the functions is not significant: in the first case, when combined with an argument whose value is NULL, the concatenation result will be NULL, and when using the second variant of the join, the null operand will simply be omitted.
When using the CONCAT function, the SQL syntaxobliges the programmer to use arguments in the order in which the operands must be connected. The syntax of the string concatenation operation is quite simple: after the CONCAT keyword, all necessary arguments or strings are delimited in comma separated by commas, and after the closing parenthesis, if necessary, the keyword AS and the name of the variable to which the result is written is indicated. The CONCAT function pattern looks like this:
CONCAT (line1, line2 [, line 3, ...]) [AS variable_name].
It should be noted that as function argumentscan be used as an operand of a numeric and string value, and some function that returns a result. For example, the operation itself is SQL CONCAT. Thus, the operation supports the principle of nesting.
How to do without using CONCAT?
In the case where there is a need to do withoutuse of the operation, or the programmer does not know the CONCAT function, SQL offers one more option for concatenating strings. To do this, you need to use the operator "+", but this approach has a number of features that must be taken into account when working with it.
When using the "+" function in caseoperands are numeric values, the result of the operation will be the ordinary addition of numbers, and if necessary, combine the number with a string without an explicit conversion, the program will generate an error. So, when the next query is executed, the result is the value of the number "6":
SELECT 1 + 2 + 3.
In other respects, both methods are identical and return the same result.