I need to take the first half of the words in a column and store in another column. I can assume there is an even number of words. I did it using a cursor and a function I found that takes a string and parses it using a delimiter into a table.
drop table #test
create table #test (id int identity, my_name varchar(128), cleaned_name varchar(128))
insert into #test (my_name) VALUES ('abcd efgh abcd1 efgh1')
insert into #test (my_name) VALUES ('name1 name2 name1a name2a')
insert into #test (my_name) VALUES ('one two one* two*')
select *
from #test
DECLARE @HalfName varchar(100)
DECLARE @i varchar(100)
set @i = 1
while @i <= (select count(*) from #test)
begin
      SELECT @HalfName = COALESCE(@HalfName + ' ', '') + aa.WORD
      FROM (select top (select count(*) / 2 from dm_generic.dbo.GETALLWORDS((select [my_name]
      from #test 
      where id = @i), ' ')) *
      from dm_generic.dbo.GETALLWORDS(
      (select [my_name]
      from #test 
      where id = @i), ' ') 
      ) aa
      update #test 
      set cleaned_name = @HalfName 
      where id = @i
      set @i = @i + 1
      set @HalfName = ''
end
select *
from #test          
I'm trying to do it without the cursor :
UPDATE bb
   SET cleaned_name =
          (SELECT COALESCE (bb.cleaned_name + ' ', '') + aa.WORD
             FROM (SELECT TOP (SELECT count (*) / 2
                                 FROM dm_generic.dbo.GETALLWORDS (
                                         (SELECT [my_name]
                                            FROM #test a
                                           WHERE a.id = bb.id),
                                         ' '))
                          *
                     FROM dm_generic.dbo.GETALLWORDS ( (SELECT [my_name]
                                                          FROM #test b
                                                         WHERE b.id = bb.id),
                                                      ' ')) aa)
  FROM #test bb
what I'm getting is :
Msg 512, Level 16, State 1, Line 1
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The statement has been terminated.
any help will be appreciated.
Thanks to all responders, I finally used this solution by @BradC to brew my own, here it is :
update updated
set cleaned_name = (
SELECT Clean
FROM #test AS extern
CROSS APPLY
( 
select TOP (SELECT count (*) / 2 
            FROM dm_generic.dbo.GETALLWORDS (
                                           (SELECT [my_name]
                                            FROM #test a
                                            WHERE a.id = extern.id), ' '))
WORD + ' '
FROM dm_generic.dbo.GETALLWORDS (
         (SELECT [my_name]
            FROM #test a
           WHERE a.id = extern.id),
         ' ')
    FOR XML PATH('')
) pre_trimmed (Clean)
where extern.id = updated.id)
from #test updated
@Nikola Markovinović solution works great as well.
 
     
     
    