Today's coding issue involves 2 Integer fields that need to be concatenated into 1 big field and that field needs to be Varchar. To accomplish this, you will need to Cast each Integer as a Varchar then concatenate them.
For Example, in an SQL table called tblPetOwners you have 2 fields (OwnerNumber & PetNumber) which are Integers and wish to join them with an '&' in between them. To do this, you have to CAST them both as Varchar before concatenating them. Below is the best way I have came across to accomplish this task and I show you the data in the table, the code itself, followed by the results.
Table tblPetOwners Data:
OwnerNumber | PetNumber | |
223122 | 543224 | |
643233 | 235432 | |
332342 | 654523 |
Example Code:
SELECT CAST(OwnerNumber AS VARCHAR(10) )
+ ' & ' +
CAST(PetNumber AS VARCHAR(10) ) OwnerAndPet, OwnerNumber, PetNumber
FROM tblPetOwners
Results Data:
OwnerAndPet | OwnerNumber | PetNumber | ||
223122 & 543224 | 223122 | 543224 | ||
643233 & 235432 | 643233 | 235432 | ||
332342 & 654523 | 332342 | 654523 |