you can refer the below queries to get the same-
select FirstName +' '+ MiddleName +' ' + Lastname as Name from TableName.
select CONCAT(FirstName , ' ' , MiddleName , ' ' , Lastname) as Name from
TableName
select Isnull(FirstName,' ') +' '+ Isnull(MiddleName,' ')+' '+ Isnull(Lastname,' ')
from TableName.
Note: Point 1 query return if all columns have some value if anyone is null or empty then it will return null for all, means Name will return "NULL" value.
To avoid the point number 1, you can use point number 2 or point number 3 -
We can use IsNull
or CONCAT
keyword to get the same.
If anyone containing null value then ' ' (blank space) will add with next value.
Let's say you want to create a single Full Name column by combining two other columns, First Name and Last Name. To combine first and last names, use the CONCATENATE function or the ampersand (&) operator.
For Oracle it is : Select firstname+' '+lastname from emp; or select concat(firstname,lastname) Name from emp; (As far as I remember concat in oracle can not take more than two arguments). So if you want to concatenate more than two fields it is better to use the concatenation operator.
RTRIM
and LTRIM
will strip any white-space on each end.CONCAT
to append the name segments togetherCOALESCE
to replace NULL
with an empty stringFormatted for readability
SELECT
RTRIM(LTRIM(
CONCAT(
COALESCE(FirstName + ' ', '')
, COALESCE(MiddleName + ' ', '')
, COALESCE(Lastname, '')
)
)) AS Name
FROM TableName
Replace function removes two character white-spaces while concatenating First, Middle and Last Name.
SQL2016 and above compatible code
SELECT REPLACE(CONCAT(FirstName+' ',MiddleName+' ',LastName+' '),' ',' ')
AS EmployeeName
FROM dbo.Employee
SQL SERVER 2017 Compatible code:
SELECT REPLACE(CONCAT_WS(' ',FirstName,MiddleName,LastName),' ',' ')AS
EmployeeName FROM dbo.Employee
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With