Concatenation of strings
1,mysql
In Java, C # and other programming languages, the concatenation of strings can be achieved by the plus "+", such as: "1" + "3", "a" + "B".
You can also use the plus sign "+" to connect two strings in MySQL, such as the following SQL:
SQL code
- SELECT ' + ' + ', fage+' 1 ' from t_employee
Execution results
SQL code
- ' + ' + 'fage+ '1 '
- 45 26
- 45 29
- 45 24
- 45 26
- 45 29
- 45 28
- 45 24
- 45 29
- 45 23
MySQL attempts to convert the field value at both ends of the plus sign to a number type, and if the conversion fails, it considers the field value to be
0, for example, we execute the following SQL statement:
SQL code
- SELECT ' abc ' +' 123 ', fage+' a ' from t_employee
After execution we can see the following results in the output:
HTML code
- ' abc ' + ' 123 ' fage+ ' a '
- 123 25
- 123 28
- 123 23
- 123 25
- 123 28
- 123 27
- 123 23
- 123 28
- 123 22
string concatenation in MySQL to use the concat function, the concat function supports one or more parameters,
Parameter types can be string types or non-string types, and for non-string type parameters MySQL will attempt to
Converting it to a string type, the Concat function will stitch all parameters into a string in the order of the parameters as
The return value. For example, the following SQL statement is used to query the user's multiple field information in the form of a computed field:
SQL code
- SELECT CONCAT (' work number: ', Fnumber,' employee's Happiness Index: ', fsalary/(FAge-21))
From T_employee execution, we can see the following results in the output:
Java code
- CONCAT (' work number: ', Fnumber,' employee's Happiness Index: ', fsalary/(fage-))
- Work Number: DEV001 's employee's Happiness index:2075.000000
- Work Number: DEV002 's employee's Happiness index:328.685714
- Work Number: HR001 's employee's Happiness index:1100.440000
Another function Concat_ws for string stitching is provided in MySQL,
Concat_ws can add a specified delimiter between the strings to be stitched, and its first parameter value is the one used
The remaining parameters are string values to be stitched, such as executing the following SQL:
SQL code
- SELECT Concat_ws (', ', fnumber,fage,fdepartment,fsalary) from t_employee
After execution we can see the following results in the output:
Java code
- Concat_ws (', ', fnumber,fage,fdepartment,fsalary)
- DEV001,25,development,8300.00
- DEV002,28,development,2300.80
- HR001,23,humanresource,2200.88
- HR002,25,humanresource,5200.36
- IT001,28,infotech,3900.00
- IT002,27,infotech,2800.00
- SALES001,23,sales,5000.00
- SALES002,28,sales,6200.00
- SALES003,22,sales,1200.00
2,oracle
Using "| |" in Oracle string concatenation, which is used in the same way as the plus sign "+" in MSSQLServer.
For example, execute the following SQL statement:
SQL code
- SELECT ' work number is ' | | fnumber| | ' employee's name is ' | | FName from t_employee
- WHERE FName is not NULL
After execution we can see the following results in the output:
HTML code
- Work number is | | fnumber| | The Employee name is | | FNAME
- Tom is the employee whose work number is DEV001
- Employee name is DEV002, Jerry
- Employee named John, SALES001
- Employee named Kerry, SALES002
- Employee named stone for work number SALES003
- Employee named HR001, Jane
- Employee named HR002, Tina
- Employee named Smith for work number IT001
In addition to the "| |", Oracle supports string concatenation using the concat () function, such as executing the following SQL statement:
SQL code
- SELECT CONCAT (' work number: ', fnumber) from t_employee
After execution we can see the following results in the output:
HTML code
- CONCAT (Work No.:, Fnumber)
- Work No.: DEV001
- Work No.: DEV002
- Work No.: HR001
- Work No.: HR002
- Work No.: IT001
- Work No.: IT002
- Work No.: SALES001
- Work No.: SALES002
- Work No.: SALES003
If the value connected in Concat is not a string, Oracle attempts to convert it to a string.
Unlike MySQL's concat () function, Oracle's CONCAT () function supports only two parameters and does not support two
Stitching on the string. If you are stitching multiple strings, you can use multiple concat () functions for nesting.
SQL code
- SELECT CONCAT (CONCAT (CONCAT (' work number ', Fnumber),' employee name '), FName) from
- T_employee
- WHERE FName is not NULL
After execution we can see the following results in the output:
HTML code
- CONCAT (CONCAT (CONCAT (work number, fnumber), employee name), FNAME)
- Tom is the employee whose work number is DEV001
- Employee name is DEV002, Jerry
- Employee named John, SALES001
- Employee named Kerry, SALES002
- Employee named stone for work number SALES003
- Employee named HR001, Jane
- Employee named HR002, Tina
- Employee named Smith for work number IT001
[Concatenation of strings in the turn]mysql