How to combine two rows in sql and put one row with 0?

I have two fields in a table. One contains values ​​such as BTA, BEA, REA. The other contains values ​​such as 1,2,63,103.

I want to combine 2 fields so that they look like BTA001, BTA002, BTA063, BTA103.

Note that if the numbers do not consist of 3 characters, I want to put some 0 to the left of the number to make it equal to 3.

How can I do it? The fields in the table are called Type, which correspond to BTA, BEA, and REA, and Id is the field corresponding to 1, 2, 63, and 103.

+5
source share
3 answers
select Type + right('00' + cast(id as varchar(10)), 3)
from ...

Edit: if id can be null and you want to show null, you can do:

select Type + right('00' + cast(isnull(id, 0) as varchar(10)), 3) 
from ...
+9
source

C1 + right (('000' + cast (C2 nvarchar (10)), 3)

t1

+1

FIELD1 + RIGHT ('000' + CONVERT (VARCHAR, FIELD2), 3)

+1
source

All Articles