Concat two integers and result as a string in SQL

In table 2, the field identifiers are both int and Number as small int, and I want to combine the two fields and display as a string

eg: ID = 101 and Number = 9 output : 101.9 

Point added between identifier and number? How to execute a query in SQL?

+7
source share
2 answers

You can CAST specify the whole field in varchar and then combine them as you wish.

 DECLARE @ID INT DECLARE @Number INT SET @ID = 101 SET @Number = 9 SELECT CAST(@ID AS VARCHAR(10) ) +'.'+ CAST(@Number AS VARCHAR(10) ) 
+31
source

In SQL 2012 (and later) it is now easier for me with CONCAT (also better performance )

 SELECT CONCAT(@ID, '.', @Number) 

any NULL elements are converted to empty lines, preventing the spread of NULL, which eliminates the need to make even more complex:

 SELECT ISNULL(CAST(@ID AS VARCHAR(10) ), '') +'.'+ ISNULL(CAST(@Number AS VARCHAR(10) ) , '') 
+3
source

Source: https://habr.com/ru/post/954838/


All Articles