C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » Databases »

SQL: Combine Columns in SQL Server


Posted Date: 25 Jun 2009    Resource Type: Articles    Category: Databases
Author: Viji RAJKUMARMember Level: Diamond    
Rating: 1 out of 5Points: 7



In this article, we can see the various sql queries to combine two columns.



Concatenation Using + Operator

Syntax:

SELECT (ColumnA + ColumnB) AS ColumnZ
FROM Table;

Example


SELECT [First Name], [Last Name], [First Name] + ' ' + [Last Name] AS 'Full Name' FROM Employee

In this example we have combined First and Last Name of a person to get the Full Name.


Result


First Name Last Name Full Name

Jack Benny Jack Benny
Michael Hussain Michael Hussain
Roberts Tennison Roberts Tennison



Concatenation Using Union Operator

The sql keyword Union is used to combine the two columns.

Syntax:

SELECT ColumnA AS ColumnZ
FROM Table

UNION --Union Keyword to combine two columns

SELECT ColumnB AS ColumnZ
FROM Table
ORDER BY ColumnZ


Example

SELECT user_login AS user_idee, employee FROM employee, grpmebrs
WHERE USER_ID = user_login
UNION
SELECT secgroup AS user_idee, employee FROM employee, grpmebrs
WHERE USER_ID = user_login


This will also list the same thing.

Effectivly the result is the same.

Combine Null Columns

The Concatenation using "+" Fails when either column is Null.

Solution:

The isNull Function is used to check whether the column is Null.


Example With isNull Function


Select isnull(CAST(Customer.Contactid as nvarchar(10)),'') +'-'+ isnull(Customer.Firstname,'') +'-'+ isnull(Customer.LastName,'') as [Full Name] from Customer where customerid = 1823


Result

1823-Michael-Dizousa










Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
SQL: Combine Columns in SQL Server  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: SQL Server Joins
Previous Resource: ADO.NET and Database Interview questions
Return to Discussion Resource Index
Post New Resource
Category: Databases


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use