Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - Splicing problem of int and varchar types in Sqlserver
Splicing problem of int and varchar types in Sqlserver
1. First of all, two columns in the data table Table_ 1 are numerical values, and num 1 and num2 are of int and float types respectively. The data is as shown in the figure.

2.convert or cast function is used for data type conversion, and then "+"is used for character splicing. The converted types can be: char, nchar, varchar, nvarchar, etc.

3. The format of the convert function is: convert(varchar(20), num 1), where varchar is a string with variable length and 20 is the maximum length of the string. The advantage of using varchar is that you can avoid spaces in the results. If spaces are needed, consider using char. The following figure shows the query results.

4. The format of the cast function is: cast(num 1 as varchar(20)). For the data format, see the description in the previous step. The following figure shows the query results.

5. The results of1and 2 are exactly the same. Here, when using the "+"operator for varchar and int, varchar will be automatically converted into int, so two sets of values need to be converted.