Here is the function:
create function f_tst
(
@txt nvarchar(1000)
) returns table
as
return (with x as
(
select case when substring(@txt, number, 1) like '[a-zA-Z]' then substring(@txt, number, 1)
else '' end a,
case when substring(@txt, number, 1) like '[0-9]' then substring(@txt, number, 1)
else '' end b,
case when substring(@txt, number, 1) NOT like '[a-zA-Z0-9]' then substring(@txt, number, 1)
else '' end c
from
master..spt_values
where type = 'P'
and number < len(@txt)
)
select distinct( select [a]
from x t1
for xml path(''), type
).value('.', 'varchar(max)') [Character],
( select [c]
from x t1
for xml path(''), type
).value('.', 'varchar(max)') [Special Character] ,
( select [b]
from x t1
for xml path(''), type
).value('.', 'varchar(max)') [Numbers]
from x t)
go
You can test single line like this:
select * from f_tst('Adventure2008Works.DW@microsoft.com')
You can get the columns from a table like this:
declare @t table(txt nvarchar(1000))
insert @t values
('Adventure2008Works.DW@microsoft.com'),
('Adventure2008Wo12ks.DW@"t...')
select * from @t cross apply dbo.f_tst(txt)
Result:
txt Character Special Character Numbers
Adventure2008Works.DW@microsoft.com AdventureWorksDWmicrosoftco .@. 2008
Adventure2008Wo12ks.DW@"t... AdventureWoksDWt .@".. 200812