We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

SQL change Surname, Firstname to Firstname Surname with no comma

JCTDD
JCTDD asked
on
Medium Priority
541 Views
Last Modified: 2012-06-22
I have a table called 'Names' displaying like this

Names
Smith, John
Jane, Mary
Chong, Bob

can you please tell me the SQL code (starting with 'select') in order to display it like this:
John Smith
Mary Jane
Bob Chong
Comment
Watch Question

CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Daniel_PLDB Expert/Architect
Top Expert 2011

Commented:
Or this:

--test
declare @Names varchar(100)
set @Names='Chong, Bob'
SELECT SUBSTRING(@Names,PATINDEX('%[,]%',@Names)+1,PATINDEX('%[,]%',@Names)+1)+ ' ' + SUBSTRING(@Names,0,PATINDEX('%[,]%',@Names))

--select from table
SELECT SUBSTRING(Names,PATINDEX('%[,]%',Names)+1,PATINDEX('%[,]%',Names)+1)+ ' ' + SUBSTRING(Names,0,PATINDEX('%[,]%',Names)) FROM table_name;

Open in new window

Rikin ShahMicrosoft Dynamics CRM Consultant
CERTIFIED EXPERT

Commented:
select ltrim(substring (Names, CHARindex(',',Names)+1, len(Names))) + ' ' + left(Names, CHARindex(',',Names)-1) 
from table

Open in new window

Commented:
I would do it like this
SELECT REPLACE(Names,',','') as Names FROM tableName

Open in new window

Daniel_PLDB Expert/Architect
Top Expert 2011

Commented:
@kaminda
Yes, it could be done just by REPLACE but read question more carefully, asker wants to have Surname and Firstname switched. You are only removing a comma (,).
Alpesh PatelSolution Architect at TCS (SAFe®Agilist, TOGAF® Foundation, PSM I®, MCP, MCSD)
CERTIFIED EXPERT

Commented:
select Ltrim(rTrim(SUBSTRING('Alpesh, Patel', CHARINDEX(',', 'Alpesh, Patel') +1 ,Len('Alpesh, Patel')))) + ' ' + Ltrim(rTrim(SUBSTRING('Alpesh, Patel', 1, CHARINDEX(',', 'Alpesh, Patel') -1)))
from table_name

TRy this
Ephraim WangoyaSoftware Engineer
CERTIFIED EXPERT

Commented:
try
Ephraim WangoyaSoftware Engineer
CERTIFIED EXPERT

Commented:
Sorry, here
select RIGHT(names, LEN(names) -CHARINDEX(',', names)) + ' ' +
       LEFT(names, CHARINDEX(',', names)-1)

Open in new window

Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.