Inverta a ordem das palavras no TSQL
Gostaria de saber como (se possível) reverter a ordem depalavra retornado de uma string TSQL (varchar
Eu sei sobre o TSQLREVERSE
, mas também inverte as letras da palavra, por exemplo:
Input>We want to tell you we all love StackOverflow
Output>wolfrevOkcatS evol lla ew uoy llet ot tnaw eW
Desejo realmente obter o seguinte no TSQL:
Input>We want to tell you we all love StackOverflow
Output>Stackoverflow love all we you tell to want We
A única pergunta ligeiramente semelhante que encontrei em qualquer lugar foiest, no entanto, isso inclui dividir cadeias separadas por vírgula, o que não preciso faze
Tenho certeza de que há uma maneira de conseguir o que foi dito acima, mesmo que seja uma função personalizada ou uma função SQL-CLR, qualquer ajuda seria muito apreciad
Editar
Eu consegui dividir minha string usando o seguinte:
-- Create a space delimited string for testing
declare @str varchar(max)
select @str = 'We want to tell you we all love StackOverflow'
-- XML tag the string by replacing spaces with </x><x> tags
declare @xml xml
select @xml = cast('<x><![CDATA['+ replace(@str,' ',']]></x><x><![CDATA[') + ']]></x>' as xml)
-- Finally select values from nodes <x> and trim at the same time
select ltrim(rtrim(mynode.value('.[1]', 'nvarchar(50)'))) as Code
from (select @xml doc) xx
cross apply doc.nodes('/x') (mynode)
O problema agora é tentar juntar tudo novamente em uma sequência em ordem decrescente (DESC).