-3

My table contains data as follows

COLA
-------------------------
Today is sunday
Hi how are you
Hello i am bharadwaj
Hi i am fine
Help me out

I Need output like as follows

COLA
-------------------------
Sunday is today
You are how hi
Bharadwaj am i hello
Fine am i hi
Out me help

James Z
  • 12,209
  • 10
  • 24
  • 44
bharath
  • 7
  • 2

1 Answers1

0

This isn't too bad if you have a string splitter. I like this one, especially for this purpose. Taking the data you posted you need another column for the primary key. Then you can do this entirely set based, no looping required like in the answer proposed as a duplicate.

This looks a LOT like homework but it also was a fun challenge on a Friday so I decided to whip something together.

declare @Something table
(
    SomeValue varchar(50)
    , SomeKey int identity
)

insert @Something
select 'Today is sunday' union all
select 'Hi how are you' union all
select 'Hello i am bharadwaj' union all
select 'Hi i am fine' union all
select 'Help me out';

with ParsedWords as
(
    select SomeKey
        , Item
        , x.ItemNumber
    from @Something 
    cross apply dbo.DelimitedSplit8K(SomeValue, ' ') x
)
, ReversedWords as
(
    select SomeKey
        , LOWER(STUFF((select ' ' + p2.Item
                from ParsedWords p2 
                where p2.SomeKey = p.SomeKey
                order by p2.ItemNumber desc
                for XML PATH('')), 1, 1, '')) as Reversed
    from ParsedWords p
    group by p.SomeKey
)

select UPPER(LEFT(Reversed, 1)) + LOWER(SUBSTRING(Reversed, 2, len(Reversed)))
from ReversedWords rw
order by rw.SomeKey
Sean Lange
  • 33,028
  • 3
  • 25
  • 40