• subramaniam.chandrasekar - Tuesday, February 6, 2018 12:34 AM

    PSB - Thursday, February 1, 2018 9:48 AM

    Hi

     I want to sort a column value alphabetically which is already in a sql table to something like below :

    Tamesha Rios, Damian Richard, Tim David     --> to Damien Richard, Tamesha Rios, Tim David

    Thanks,
    PSB

    I've created a test table and small static script which produces your desired output, 


    SELECT *  INTO #TestTable FROM (VALUES
       ('Tamesha Rios,Damian Richard,Tim David')
       )v(CSVField);

    SELECT CSVField,  PARSENAME(REPLACE(CSVField,',','.'),2) + ',' + PARSENAME(REPLACE(CSVField,',','.'),3) + ',' + PARSENAME(REPLACE(CSVField,',','.'),1) 'Name'  FROM #TestTable

    For realtime dynamic conversion, you'd need to create an user defined function which does the same. Please keep an eye on the performance of your function as well while executing....

    That's nice but it will fail if there are more than 4 names.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)