declare @txt varchar(1000) ='cc:x@gmail.com cc:y@gmail.com james cc:q@gmail.com'
;with split as
(
select 1 f, charindex(' ', @txt+ ' ', 2) t
union all
select t+1, charindex(' ', @txt+ ' ', t+1)
from split
where charindex(' ', @txt+ ' ', t+1) > f
)
select substring(@txt, f, t-f) from split
where not substring(@txt, f, t-f) like '%@%'
Результат:
james
РЕДАКТИРОВАТЬ:
Я изменил sql в соответствии с вашими требованиями:
create function f_returnnames(@txt varchar(1000))
returns varchar(1000)
as
begin
declare @returntext varchar(1000)
;with split as
(
select 1 f, charindex(' ', @txt+ ' ', 2) t
union all
select t+1, charindex(' ', @txt+ ' ', t+1)
from split
where charindex(' ', @txt+ ' ', t+1) > f
)
select @returntext = coalesce(@returntext + ' ', '') + substring(@txt, f, t-f) from split
where not substring(@txt, f, t-f) like '%@%'
return @returntext
end
go
Вы можете проверить это здесь:
select (select dbo.f_returnnames(column_name)) names
from
(select 'cc:x@gmail.com cc:y@gmail.com james cc:q@gmail.com' column_name) a