字符串的逆序

Reverse Order of String

我需要在 SQL 服务器的一列中反转字符串的顺序。

示例:

"Smith,John" expected to "John Smith".
"Depp,Johnny" expected to "Johnny Depp".

知道如何用逗号分隔,然后用 space 分隔吗?

谢谢!

我推荐"reconstruction":

select concat(stuff(str, 1, charindex(',', str), ''), ' ',
              left(str, chardindex(',', str) - 1)
             ) as reversed_names

分解和重组的一种方法:

select concat(substring(col, charindex(',', col)+1, len(col)), ' ',
              left(col, charindex(',', col)-1)
             )

您可以按照以下方式进行

SELECT S, CONCAT(
                 REPLACE(S, LEFT(S, CHARINDEX(',', S)), ''), ' ', 
                 LEFT(S, CHARINDEX(',', S)-1)
                )
FROM (VALUES('Smith,John'), ('Depp,Johnny')) T(S)