Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Query to concatenate two column values in SQL Server

Jul 19, 2012
Introduction:

In this article I will explain how to write a query to concatenate two columns of data to single column in datatable using SQL server.

Description:

column in datatable in SQL Server and many articles relating to SQL Server. Now I will explain how to write a query to concatenate two columns of data into single column in SQL Server. Here I will explain with one example for that I have one table UserDetails like as shown below

Syntax to concatenate two columns

If we want to concatenate two columns into single column value that syntax will be like as shown


SELECT (CONVERT(varchar(50),Column1)+' '+CONVERT(varchar(50),Column2)) AS ConcatnatedColumn FROM TableName
Ex:


SELECT UserName,FirstName,LastName,(CONVERT(varchar(50),FirstName)+' '+CONVERT(varchar(50),LastName)) AS ConcatnatedColumn FROM UserDetails
OutPut

We can concatenate in another way also

Another way Syntax to concatenate two columns

If we want to convert characters to lowercase that syntax will be like this


SELECT (CAST(ColumnName1 AS varchar(50))+' '+CAST(ColumnName2 AS varchar(50))) AS ConcatnatedColumn FROM TABLENAME
Ex:


SELECT UserName,FirstName,LastName,(CAST(FirstName AS varchar(50))+' '+CAST(LastName AS varchar(50))) AS ConcatnatedColumn FROM UserDetails
OutPut


If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

6 comments :

Santhosha said...

Nice Article Sir, Thank You.

Unknown said...

Hi Suresh,

here i dont see any difference in both ways, as we know that the things which we can do with CONVERT function which can also be possible CAST function in maximum cases at least

Unknown said...

thq i have a on Dought how to insert a values in two tables by using single query

Unknown said...

sir,

how to insert a textboxes,dropdown list dynamically with out using grid view give me a reply as early as possible.

Anonymous said...

I am Very Very Thank full to You
Nice Way of Representation
-Vikram

Unknown said...

Thank you sir for the info it actually help me in my C# code

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.