Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to concatenate strings in MYSQL

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

MYSQL how to concatenate strings, I believe that many inexperienced people do not know what to do, so this article summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

How to concatenate MYSQL strings

You can also use the plus sign "+" in MYSQL to concatenate two strings, such as the following SQL:

SELECT'12'+'33',FAge+'1'FROMT_Employee

Are you surprised to take a closer look at the first column? The result of this column is not the "1233" we want, but the "12"

The two strings "33" and "33" are treated as numbers to find the sum of two numbers; the same is true of concatenating a number with a MYSQL string with the plus sign "+", such as the second column here.

In MYSQL, when two fields (or more fields) are joined with the plus sign "+", MYSQL attempts to convert the field value to a numeric type (if the conversion fails, the field value is considered to be 0), and then performs the addition operation of the field. Therefore, when calculating'12 percent 33', MYSQL attempts to convert the strings "12" and "33" to 12 and 33 of the numeric type, and then calculates the value of 12 percent 33, which is why we get the result of 45. Similarly, when calculating FAge+'1', because FAge is a numeric type, it does not need to be converted, while'1' is a string type, so MYSQL attempts to convert'1' to the number 1, and then calculates FAge+1 as the value of the calculated column.

How to concatenate MYSQL strings

MYSQL will try to convert the field values at both ends of the plus sign to a numeric type. If the conversion fails, the field value will be 0, for example, we execute the following SQL statement:

SELECT'abc'+'123',FAge+'a'FROMT_Employee

CONCAT function is used for string stitching in MYSQL. CONCAT function supports one or more parameters. The parameter type can be string type or non-string type. For non-string type parameters, MYSQL will try to convert them to string type. CONCAT function will concatenate all parameters into a string in the order of 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 calculated field:

SELECTCONCAT ('job number:', happiness index of FNumber,' 's employees:', FSalary/ (FAge-21))

FROMT_Employee

CONCAT supports the use of only one parameter, and CONCAT can be seen as a function that attempts to convert the parameter value to a string type value. MYSQL also provides another function for string concatenation, CONCAT_WS,CONCAT_WS, which can add a specified delimiter between the strings to be concatenated. Its first parameter value is the delimiter to be adopted, and the remaining parameters are the string values to be concatenated, such as executing the following SQL:

SELECTCONCAT_WS (',', FNumber,FAge,FDepartment,FSalary) FROMT_Employee

Unlike MYSQL, you can use the plus sign "+" to concatenate strings directly in MSSQLServer. For example, execute the following SQL statement:

The name of the employee whose SELECT' work number is'+ FNumber+' is'+ FnameFROMT_Employee

WHEREFNameISNOTNULL

After reading the above, have you mastered how to concatenate strings in MYSQL? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report