Saturday 30 July 2011

Concatenation in SQL


An operator in a string expression that concatenates two or more character or binary strings, columns, or a combination of strings and column names into one expression (a string operator).
It is a simple way to display different columns value in single one.
Note: ‘+’ sign is used for concatenation.
Supposew we have a table with name tbl_StudentDetails with following fields:
FirstName
MiddleName
LastName
DOB                 etc.

Now try for simple query for display data of tbl_studentDetails, we write something like this;
Select FirstName, , MiddleName, LastName, DOB from tbl_StudentDetails

The Result will look like this;
FirstName
MiddleName
LastName
DOB
Anil
Mohan
Sharma
2-2-1980
Manoj
Kumar
Singh
5-12-1983
Rahul
Tripathi

25-9-1990

Now I want to display whole result in two columns like StudentName and DOB. StudentName Column contains whole information ie, firstname, middleName, LastName.

We write this query in such manner;

Select (FirstName +’ ‘+ MiddleName+’ ‘+ LastName) As StudentName , DOB
from tbl_StudentDetails

The Result will look like this;

StudentName
DOB
Anil Mohan Sharma
2-2-1980
Manoj Kumar Singh
5-12-1983
Rahul Tripathi
25-9-1990


No comments:

Post a Comment