Click here to Skip to main content
15,891,597 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
i use this code to collect the name


SQL
SELECT        ISNULL(Col2, '') + ' ' + ISNULL(Col3, '') + ' ' + ISNULL(Col4, '') + ' ' + ISNULL(Col5, '') + ' ' + ISNULL(Col6, '') + ' ' + ISNULL(Col7, '') + ' ' + ISNULL(Col8, '') + ' ' + ISNULL(Col9, '') + ' ' + ISNULL(Col10, '') AS Fullname
FROM            dbo.tabl


What I have tried:

and i try to remove the space form it by this code
bout it didn't work with views so what should i do
GO

UPDATE tabl SET Fullname= LTRIM(Fullname)
GO
Posted
Updated 31-Dec-18 3:10am
v2
Comments
[no name] 31-Dec-18 7:14am    
Not sure wheter LTRIM is really what you Need... Anyway Fullname seems not to be a column of table tabl (it is an alias fieldname for your concatenation) and therefore no Chance to update it ;)

Maybe this is what you like to do?
SELECT LTRIM( ISNULL(Col2, '') + ' ' + ISNULL(Col3, '') + ' ' + ISNULL(Col4, '') + ' ' + ISNULL(Col5, '') + ' ' + ISNULL(Col6, '') + ' ' + ISNULL(Col7, '') + ' ' + ISNULL(Col8, '') + ' ' + ISNULL(Col9, '') + ' ' + ISNULL(Col10, '')) AS Fullname
FROM dbo.tabl
el_tot93 31-Dec-18 7:22am    
THAT WHAT I LOOKING FOR THX

Ok then, Looks like you are really looking for this:
SELECT LTRIM( 
   ISNULL(Col2, '') + ' ' + ISNULL(Col3, '') + ' ' + ISNULL(Col4, '') + ' ' + 
   ISNULL(Col5, '') + ' ' + ISNULL(Col6, '') + ' ' + ISNULL(Col7, '') + ' ' + 
   ISNULL(Col8, '') + ' ' + ISNULL(Col9, '') + ' ' + ISNULL(Col10, '')
   ) AS Fullname
 FROM dbo.tabl
 
Share this answer
 
v2
Just to give you an alternative. If the full name is used in multiple queries, it could be a good idea to define it as a computed column in the database. In other words
SQL
ALTER TABLE dbo.tabl
  ADD FullName AS RTRIM( LTRIM( 
   ISNULL(Col2, '') + ' ' + ISNULL(Col3, '') + ' ' + ISNULL(Col4, '') + ' ' + 
   ISNULL(Col5, '') + ' ' + ISNULL(Col6, '') + ' ' + ISNULL(Col7, '') + ' ' + 
   ISNULL(Col8, '') + ' ' + ISNULL(Col9, '') + ' ' + ISNULL(Col10, '')
   ));

After this, you can use the FullName column in your queries without repeating the formula everywhere:
SQL
SELECT FullName FROM dbo.tabl;



As a side note, I would suggest using meaningful names for the columns instead of col1, col2, ... Later when you come back to the code, it's hard to remember which column was which. If these are part of names perhaps something like NamePart1, NamePart2, ...
 
Share this answer
 
Comments
[no name] 31-Dec-18 9:14am    
Very good alternative and comments, a 5
Wendelius 1-Jan-19 7:29am    
Thank you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900