Click here to Skip to main content
12,454,164 members (56,789 online)
Rate this:
 
Please Sign up or sign in to vote.
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 3-Jan-12 20:37pm
gani77871.5K
Updated 3-Jan-12 20:42pm
Mehdi Gholam320.2K
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Use the following :

select Convert(nvarchar(50),ID)+'-'+Convert(nvarchar(50),Year) as ID_Year from tablename
  Permalink  
Comments
Member 11071777 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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Try also
SELECT (ID + '-' + year) AS ID_Year
FROM tablename
  Permalink  
Comments
Member 11149016 17-Dec-15 4:10am
   
It throws error . it asks for conversion of datatype between two columns
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

u can use Sql Query like:-

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

Hope, this will help you...
  Permalink  
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....
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

SELECT Title,
FirstName,
lastName,
ISNULL(Title,'') + ' ' + ISNULL(FirstName,'') + ' ' + ISNULL(LastName,'') as FullName
FROM Customer
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

select distinct cast(ID+ '-' + Year as varchar(100)) as newcolumn from tablename
  Permalink  
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...

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160826.1 | Last Updated 20 Dec 2014
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100