Need to Swap the word in SQL

  • Hi all,

    I have a column with the name of Author more than 1 lakh, examples value

    Friel, Joe

    But i need to show this value in front end like Joe Friel

    can any tell me how to manipulate that in TSQL Common separator is ,(Comma)

  • Try this sir:

    declare @stringtable table

    ( string varchar(20) )

    insert into @stringtable select 'Friel, Joe'

    select RIGHT(string,( len(string ) - charindex(',',string)) )+ ' ' + left (string,( charindex(',',string) -1 ))

    from @stringtable

  • Thank u sir,

    I forgot the charindex functions.. thanks for you output...

  • Saravanan_tvr (7/1/2010)


    Thank u sir,

    I forgot the charindex functions.. thanks for you output...

    No issues, welcome saravanan!

  • Just bear in mind that the above query will break if there are any rows in the table that don't have a , somewhere in that string. If there's a possibility of that happening, add a where clause that limits to rows that do have a comma in the string.

    declare @stringtable table

    ( string varchar(20) )

    insert into @stringtable values ('Friel, Joe')

    insert into @stringtable values ('Someone Else')

    select RIGHT(string,( len(string ) - charindex(',',string)) )+ ' ' + left (string,( charindex(',',string) -1 ))

    from @stringtable

    Msg 536, Level 16, State 5, Line 6

    Invalid length parameter passed to the SUBSTRING function.

    select RIGHT(string,( len(string ) - charindex(',',string)) )+ ' ' + left (string,( charindex(',',string) -1 ))

    from @stringtable

    where charindex(',',string) > 0

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Small changes i done from your query

    SELECT TOP 100 Author,LEFT(author,CHARINDEX(',',author)-1)AS FIRST_PART,

    SUBSTRING(author, CHARINDEX(',',author)+1,LEN(author) ) AS SECOND_PART

    FROM Table WHERE Author LIKE '%,%'

  • Saravanan_tvr (7/1/2010)


    Small changes i done from your query

    SELECT TOP 100 Author,LEFT(author,CHARINDEX(',',author)-1)AS FIRST_PART,

    SUBSTRING(author, CHARINDEX(',',author)+1,LEN(author) ) AS SECOND_PART

    FROM CTBR WHERE Author LIKE '%,%'

    Yes, I was just in the process of writing you needed to add

    ( Charindex(',', string) ) - 1 ))

    To get rid of the "space" that was prefixing the cell.

    RIGHT(string, ( Len(string) - ( Charindex(',', string) ) - 1 )) + ' ' + LEFT(string, ( Charindex(',', string) - 1 ))


    Forever trying to learn
    My blog - http://www.cadavre.co.uk/
    For better, quicker answers on T-SQL questions, click on the following...http://www.sqlservercentral.com/articles/Best+Practices/61537/
    For better, quicker answers on SQL Server performance related questions, click on the following...http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply