3

I have two int columns in my table.

I want to concatenate them like a string

for example : A = 12345 , B = 2

I want to return 123452.

How can I do that ?

3
  • But why do you want to do that?!? First cast to char, then concat. Commented May 25, 2015 at 9:29
  • or use concat. SELECT CONCAT(12345,2) Commented May 25, 2015 at 9:42
  • possible duplicate of stackoverflow.com/questions/19067649/… Commented May 25, 2015 at 9:54

4 Answers 4

5

Try like this:

select CAST(A as varchar(10)) + CAST(B as varchar(10))

SQL DEMO

Sign up to request clarification or add additional context in comments.

2 Comments

I did this, but it make my query slow, do u have a quicker query to do that?
@BrMe:- I think you are running a different query then the one which I posted. What is the query which you are trying and which is taking time? Also the above query is quick enough, I dont think that you can achieve this any more faster than this.
0

You can use something like below

SELECT NUMBER_COL1,
       NUMBER_COL2,
       CAST(NUMBER_COL1 AS VARCHAR(10)) + CAST(NUMBER_COL1 AS VARCHAR(10)) JOINED_NUM
  FROM TABLE;

Comments

0

Use This Code:

declare @a int
declare @b int

select @a='12345'
select @b='2'

select cast(@a as char(10))+cast(@b as char(10)) as Concatenation

Comments

0

But you can use math functions too. For example, in ORACLE SQL:

SELECT A * Power( 10, Length(B) ) + B FROM DUAL;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.