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.
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:
In
previous post I explained how to alter or modify or delete or
change already existing
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. |
|||
|
|||
6 comments :
Nice Article Sir, Thank You.
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
thq i have a on Dought how to insert a values in two tables by using single query
sir,
how to insert a textboxes,dropdown list dynamically with out using grid view give me a reply as early as possible.
I am Very Very Thank full to You
Nice Way of Representation
-Vikram
Thank you sir for the info it actually help me in my C# code
Note: Only a member of this blog may post a comment.