2014-04-08 109 views

回答

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 

您可以測試一行是這樣的:

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 
+0

簡直太神奇了.... –

1

我會在.NET RegEx類上創建CLR包裝。這裏是an example

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 = '[email protected]' 
SET @strAlphaNumeric = '[email protected]' 
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