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
 changing the order of name

Author  Topic 

rj8519
Starting Member

2 Posts

Posted - 2011-07-09 : 21:23:02
i am stuck with this problem please help me with it

i have to write a SQL statement that will list the contents as:

Harry Smith

where as table has just one column as name which stores names as

Smith,Harry

Thanks.

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2011-07-09 : 22:08:58
Is there always exactly 1 comma?


Declare @name varchar(20)
Set @name = 'Smith,Harry'

Select
right(@name,charindex(',',reverse(@name))-1) +
' ' +
left(@name,charindex(',',@name)-1)


Corey

I Has Returned!!
Go to Top of Page

jcelko
Esteemed SQL Purist

547 Posts

Posted - 2011-07-10 : 15:50:05
Why? Display formatting is done in the front end and not in the database. A better question is how you can put the first and last names into their own columns, if it is important to your database. As usual, bad DDL leads to extra DML.

--CELKO--
Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Go to Top of Page
   

- Advertisement -