order by word in SQL [closed]
For E.g
I have word "John Roshan" i want to arrange this order by first 开发者_运维百科alphbet of the each word that means the output of the above word should be "Roshan John".
I want to do it with SQL
Please help me its urget.
Thanks in advance
declare @S varchar(50)
set @S = 'John Roshan 2000'
;with cte as
(
select
1 as P1,
charindex(' ', @S+' ', 1) as P2
union all
select
C.P2+1,
charindex(' ', @S+' ', C.P2+1) as P2
from cte as C
where charindex(' ', @S+' ', C.P2+1) > 0
)
select
(
select substring(@S, P1, P2-P1)+' '
from cte
order by substring(@S, P1, P2-P1)
for xml path(''), type
).value('.', 'varchar(50)')
精彩评论