Question: I have this column

```
NAME
John Stephenson
James Martin
Anna Corelia

```

How I can select this column to this?

```
NAME
Stephenson, John
Martin, James
Corelia, Anna 

```

Answer: 
Here is a solution including how I got the final output column.

This will handle three part names, like "Rip Van Winkle", also names with no white space and will strip (trim) any leading/trailing white space from the names.

```
DECLARE @NAMES TABLE (NAME NVARCHAR(MAX))
INSERT INTO @NAMES (NAME) VALUES ('John Stephenson'),('James Martin'),('Anna Corelia'),('Rip Van Winkle'),('Sally')

SELECT *, 
       CHARINDEX(' ',LTRIM(RTRIM(NAME))) [SpaceLocation],
       CASE WHEN CHARINDEX(' ',LTRIM(RTRIM(NAME)))>0 THEN RIGHT(LTRIM(RTRIM(NAME)), LEN(LTRIM(RTRIM(NAME)))-CHARINDEX(' ',LTRIM(RTRIM(NAME)))) END [LastName],
       CASE WHEN CHARINDEX(' ',LTRIM(RTRIM(NAME)))>0 THEN LEFT(LTRIM(RTRIM(NAME)),CHARINDEX(' ',LTRIM(RTRIM(LTRIM(RTRIM(NAME)))))-1) END [FirstName],
       CASE WHEN CHARINDEX(' ',LTRIM(RTRIM(NAME)))>0 THEN RIGHT(LTRIM(RTRIM(NAME)), LEN(LTRIM(RTRIM(NAME)))-CHARINDEX(' ',LTRIM(RTRIM(NAME)))) + ', ' +LEFT(LTRIM(RTRIM(NAME)),CHARINDEX(' ',LTRIM(RTRIM(NAME)))-1) ELSE LTRIM(RTRIM(NAME)) END [FinalOutput]
FROM @NAMES

```
There is no a single trim function but you can 

```
 select   ltrim(rtrim(colname)) from tablename 

```

which will TRIM both sides.

but what you need is:

```
declare @name varchar(300) ='Stephenson John';
Select  

Substring(@name, 1,Charindex(' ', @name)-1) +
', '  +
Substring(@name, Charindex(' ', @name)+1, LEN(@name))
 as name

```

Sorry I miss post