Pregunta

I have been trying to combine the first and last names into a single field with a space between example is John Doe.

When I do the following concat I get JohnDoe and not John Doe

SELECT     LTRIM(RTRIM(dbo.gpEmployeeList.firstName)) AS [First Name], LTRIM(RTRIM(dbo.gpEmployeeList.LASTNAME)) AS [Last Name], 
                      dbo.gpEmployeeList.SecurityCode AS [Security Code], dbo.gpEmployeeList.PTO AS [Available PTO], { fn CONCAT(LTRIM(RTRIM(dbo.gpEmployeeList.firstName)), 
                      dbo.gpEmployeeList.LASTNAME) } AS DisplayName
FROM         dbo.MonthName INNER JOIN
                      dbo.gpEmployeeList ON dbo.MonthName.monthNumber = dbo.gpEmployeeList.BIRTHMONTH LEFT OUTER JOIN
                      dbo.domainAccounts ON dbo.gpEmployeeList.LASTNAME = dbo.domainAccounts.sn

Any suggestions for a fix? I have to do the trim to get rid of a mess of spaces between the names.

¿Fue útil?

Solución

CONCAT is a SQL Sever 2012 function and does not exists in previous version. If you run the sqlfiddle that sgeddes created, which is setup as MS SQL Sever 2012, the query runs. However, if you change it to MS SQL sever 2008 and rebuild the schema it throws an error. I assume you're not running 2012 because you're getting a concat error.

http://msdn.microsoft.com/en-us/library/hh231515.aspx

Wrapping the function in brackets will make it work but I don't see the need.

SOCALNoob hit the nail on the head. The + will combine columns and text together quite nicely. I don't see why the below would not work.

 LTRIM(RTRIM(dbo.gpEmployeeList.firstName)) + ' ' + dbo.gpEmployeeList.LASTNAME) AS DisplayName

Otros consejos

You can use either ones :

1- SELECT LTRIM(RTRIM(firstName)) AS [First Name], LTRIM(RTRIM(lastName)) AS [Last Name], { fn CONCAT ({ fn CONCAT(firstName, ' ')}, lastName) } AS DisplayName FROM yourTable

OR

2- SELECT LTRIM(RTRIM(firstName)) AS [First Name], LTRIM(RTRIM(lastName)) AS [Last Name], firstName +' ' + lastName AS DisplayName FROM yourTable

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top