读书人

请问字符串比较有关问题

发布时间: 2012-09-01 09:33:03 作者: rapoo

请教字符串比较问题
有两组字符串 第一组第二组 比较出结果
LGALGDLGULGALGDLGU 一样的字符串
LGALGDLGULGALGULGD 一样的字符串
LGALGDLGULGDLGALGU 一样的字符串
LGALGDLGULGDLGULGA 一样的字符串
LGALGDLGULGULAGLGD 一样的字符串
LGALGDLGULGULGDLGA 一样的字符串
有没有SQL语句,能否判断出上面二组字符串是一样的字符串。规律见上。知道的告诉我下,谢谢



[解决办法]

SQL code
if object_id('[tb]') is not null drop table [tb]gocreate table [tb]([col1] varchar(9),[col2] varchar(9))insert [tb]select 'LGALGDLGU','LGALGDLGU' union allselect 'LGALGDLGU','LGALGULGD' union allselect 'LGALGDLGU','LGDLGALGU' union allselect 'LGALGDLGU','LGDLGULGA' union allselect 'LGALGDLGU','LGULAGLGD' union allselect 'LGALGDLGU','LGULGDLGA'gocreate function str_def(@s1 varchar(100),@s2 varchar(100))returns intasbegin  declare @result int  declare @t1 table(col char(1))  declare @t2 table(col char(1))  insert @t1  select substring(@s1,number,1)  from master..spt_values  where type='P' and number between 1 and len(@s1)  insert @t2  select substring(@s2,number,1)   from master..spt_values  where type='P' and number between 1 and len(@s2)  if not exists  (    select 1     from (select col,count(1) as cnt from @t1 group by col) a     where not exists    (      select 1       from (select col,count(1) as cnt from @t2 group by col) b       where a.col=b.col and a.cnt=b.cnt    )  )    set @result=1  else    set @result=0  return @resultendgoselect *,dbo.str_def(col1,col2) AS RESULT  from tb--测试结果:/*col1      col2      RESULT--------- --------- -----------LGALGDLGU LGALGDLGU 1LGALGDLGU LGALGULGD 1LGALGDLGU LGDLGALGU 1LGALGDLGU LGDLGULGA 1LGALGDLGU LGULAGLGD 1LGALGDLGU LGULGDLGA 1(6 行受影响)*/drop function str_def 

读书人网 >SQL Server

热点推荐