大家好,还有一个有趣的问题。
假设我们有一个这样的电子邮件ID: -
Adventure2008Works.DW@microsoft.com
所以现在的问题是将所有这些角色,特殊字符,数字分开并展示出来 在三个单独的列中显示如下。
Character | Special Character | Numbers
_____________________________________________________________
AdventureWorksDWmicrosoftcom | .@. | 2008
答案 0 :(得分:1)
我会在.NET RegEx类上创建CLR包装器。这是an example。
答案 1 :(得分:1)
这是功能:
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
您可以像这样测试单行:
select * from f_tst('Adventure2008Works.DW@microsoft.com')
您可以从以下表格中获取列:
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)
结果:
txt Character Special Character Numbers
Adventure2008Works.DW@microsoft.com AdventureWorksDWmicrosoftco .@. 2008
Adventure2008Wo12ks.DW@"t... AdventureWoksDWt .@".. 200812
答案 2 :(得分:0)
你可以试试这个,
DECLARE @strAlphaNumericMain VARCHAR(256)
DECLARE @strAlphaNumeric VARCHAR(256)
DECLARE @Expr1 VARCHAR(255), @Expr2 VARCHAR(255), @Expr3 VARCHAR(255)
DECLARE @OnlyCharacter VARCHAR(MAX),@OnlyNumber VARCHAR(MAX),@OnlySplChar VARCHAR(MAX)
SET @strAlphaNumericMain = 'Adventure2008Works.DW@microsoft.com'
SET @strAlphaNumeric = 'Adventure2008Works.DW@microsoft.com'
SET @Expr1 = '%['+'@.'+']%'
SET @Expr2 = '%['+'a-z0-9'+']%'
SET @Expr3 = '%['+'^0-9'+']%'
WHILE PatIndex(@Expr1, @strAlphaNumeric) > 0
SET @strAlphaNumeric = Stuff(@strAlphaNumeric, PatIndex(@Expr1, @strAlphaNumeric), 1, '')
SET @OnlyCharacter = @strAlphaNumeric
SET @strAlphaNumeric = @strAlphaNumericMain
WHILE PatIndex(@Expr2, @strAlphaNumeric) > 0
SET @strAlphaNumeric = Stuff(@strAlphaNumeric, PatIndex(@Expr2, @strAlphaNumeric), 1, '')
SET @OnlyNumber = @strAlphaNumeric
SET @strAlphaNumeric = @strAlphaNumericMain
WHILE PatIndex(@Expr3, @strAlphaNumeric) > 0
SET @strAlphaNumeric = Stuff(@strAlphaNumeric, PatIndex(@Expr3, @strAlphaNumeric), 1, '')
SET @OnlySplChar = @strAlphaNumeric
SELECT @strAlphaNumericMain, @OnlyCharacter,@OnlyNumber,@OnlySplChar
主要来源:here