Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
2.78/5 (4 votes)
Ho to concatenate two columns in SQL Server 2008.

Example Below
-------------
ID(int)     Year(int)     
-------     ---------
10           2008
20           2009
30           2010

Output should come as below
ID_Year (Output)
-------
10-2008
20-2009
30-2010

How to query in SQL Server 2008.
Posted
Updated 1-Dec-19 23:27pm
v2

Use the following :

SQL
select Convert(nvarchar(50),ID)+'-'+Convert(nvarchar(50),Year) as ID_Year from tablename
 
Share this answer
 
Comments
Elaa_zx 8-Nov-14 22:07pm    
How to concatenate multiple tables into a single table(every table has the same column name)in SQL server 2008
Bebosh 18-Jan-15 4:57am    
Yes, this is what I want .. Million thanks
Try also
SQL
SELECT (ID + '-' + year) AS ID_Year
FROM tablename
 
Share this answer
 
Comments
Member 11149016 17-Dec-15 4:10am    
It throws error . it asks for conversion of datatype between two columns
u can use Sql Query like:-

select cast(id as varchar)+'-'+ cast(year as varchar) as ID_year

Hope, this will help you...
 
Share this answer
 
v2
Comments
CHill60 10-Dec-13 7:33am    
It is better for you to vote for the solution than posting a comment as a solution in its own right. You will only attract downvotes and reports
Jain Nishant 11-Dec-13 0:03am    
Now, u can vote for me....
SQL
select distinct cast(ID+ '-' + Year as varchar(100)) as newcolumn from tablename
 
Share this answer
 
v2
Comments
anghan22 27-Apr-13 0:16am    
i want to string and int concat plz give me..
soluction


e.g

KK-2
AA-3


above look like...
VB
SELECT Title,
FirstName,
lastName,
ISNULL(Title,'') + ' ' + ISNULL(FirstName,'') + ' ' + ISNULL(LastName,'') as FullName
FROM Customer
 
Share this answer
 
select ID || '-' || Year as ID_Year from tablename
 
Share this answer
 
Comments
CHill60 2-Dec-19 8:41am    
Post is clearly tagged as SQL Server 2008. You have supplied a solution that is not relevant to that environment (i.e. you have used the concatenation operator for postgres or Oracle (amongst others). You should also take care as (unless configured otherwise) || means "logical or" which could lead to some unexpected results.
Usually best to stick with standard Transact-SQL

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900