Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 combine two column in Select statetment?

Author  Topic 

markshen2006
Starting Member

15 Posts

Posted - 2008-04-28 : 17:06:15
Hi

I have a staff table and it has columns like firstname, lastname etc

I did the query and it works. but firstname and lastname are too close

SELECT (FirstName + Lastname) as fullname
FROM StaffList

I need the format "firstname , lastname " so I write the second query

SELECT (FirstName + " , " + Lastname) as fullname
FROM StaffList

But it doesn't work.Please help me and let me know how to make the second query work.

Thanks a lot

Mark

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-04-28 : 17:19:54
Use single quotes instead of double quotes.

SELECT FirstName + ' ' + LastName AS FullName
FROM StaffList

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-04-29 : 00:07:31
Make sure you dont have any NULL values appearing in any of your fields as concatenation with null yields nulls while CONCAT_NULL_YIELDS_NULL is ON. So its better to do like this to avoid NULLs in result
SELECT COALESCE(FirstName,'') + " , " + COALESCE(Lastname,'') as fullname
FROM StaffList

Also you dont really require the braces around concatenated field.
Go to Top of Page
   

- Advertisement -