rhk123
rhk123

Reputation: 29

Append Column Values in Alphabetical Order

Dear Stackoverflowers,

I have a table with two columns: column "A" and column "B".

Each column contains random strings with a length between 1 and 10 characters.

How do I create an update query that will alphabetically sort the values in each ROW and, if necessary, switch (update) the columns so the string in column "A" alphabetically precedes the string in column "B".

For Example:

    Starting Values:
    Col_A    Col_B
    ABC      DEF
    GHI      JKL
    PQR      MNO

    Ending Values:
    Col_A   Col_B
    ABC     DEF   alphabetically, ABC preceeds DEF, therefore, no change
    GHI     JKL   alphabetically, GHI preceeds JKL, therefore, no change
    MNO     PQR  *alphabetically, MNO preceeds PQR, therefore switch values*

Thank you!

Upvotes: 0

Views: 219

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269883

This should work:

update t
    set a = b,
        b = a
    where b < a;

Upvotes: 1

Related Questions