Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

concat two integers and result as string in SQL

In table, 2 fields ID as int and Number as small int and i want to concatenate the two fields and display as string

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

Dot to be added in between ID and Number? How to query in SQL?

like image 379
user96888 Avatar asked Sep 28 '13 13:09

user96888


People also ask

How do I concatenate a variable to a string in SQL?

Concatenates two strings and sets the string to the result of the operation. For example, if a variable @x equals 'Adventure', then @x += 'Works' takes the original value of @x, adds 'Works' to the string, and sets @x to that new value 'AdventureWorks'.

Can you concatenate integers and strings?

To concatenate a String and some integer values, you need to use the + operator. Let's say the following is the string. String str = "Demo Text"; Now, we will concatenate integer values.


1 Answers

You can CAST your integer field to varchar and then concatenate them as you want.

DECLARE @ID INT 
DECLARE @Number INT 

SET @ID = 101 
SET @Number = 9  

SELECT CAST(@ID AS VARCHAR(10) ) +'.'+ CAST(@Number AS VARCHAR(10) ) 
like image 157
Sachin Avatar answered Sep 20 '22 18:09

Sachin