下面是函數:
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
您可以測試一行是這樣的:
declare @t table(txt nvarchar(1000))
insert @t values
('[email protected]'),
('[email protected]"t...')
select * from @t cross apply dbo.f_tst(txt)
結果:
select * from f_tst('[email protected]')
可以從這樣的表中獲取的列:
txt Character Special Character Numbers
[email protected] AdventureWorksDWmicrosoftco [email protected] 2008
[email protected]"t... AdventureWoksDWt [email protected]".. 200812
簡直太神奇了.... –