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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 place end string to the front

Author  Topic 

sg2255551
Constraint Violating Yak Guru

274 Posts

Posted - 2012-12-09 : 05:22:33
hi

I have these rows data in a field that looks like this

ColA
demohello, hi
john doe, dear
do you want to go, where

And i would like to shift whatever text after the comma to the front like

hi demohello
dear john doe
where do you want to go

How should go about it? Thanks a lot

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-12-09 : 08:22:12
[code]STUFF(ColA,1,CHARINDEX(',',ColA),'') + ' ' + LEFT(ColA,CHARINDEX(',',ColA)-1)[/code]
Go to Top of Page

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2012-12-09 : 10:35:41
[code]Declare @S Varchar(20)
Set @S = 'john doe, dear'

Select ltrim(REPLACE(REVERSE(Left(REVERSE(@S),CharIndex(',',REVERSE(@S)))),',','') + ' ' + REPLACE(REVERSE(Substring(REVERSE(@S),CHARINDEX(',',REVERSE(@S)),len(@S))),',',''))[/code]
Go to Top of Page
   

- Advertisement -