SQLServer中求两个字符串的交集
时间:2021-09-03 09:18:13|栏目:MsSql|点击: 次
使用javascript的数组来计算,代码如下:
use tempdb
go
if (object_id ('fn_getArray' ) is not null )
drop function dbo . fn_getArray
go
create function fn_getArray (@ inStr1 varchar (8000 ), @ inStr2 varchar (8000 ))
returns varchar (8000 )
as
begin
declare @ str varchar (8000 )
set @ str = '
var a = [''' + replace (@ inStr1 , ',' , ''',''' )+ '''];
var b = [''' + replace (@ inStr2 , ',' , ''',''' )+ '''];
var c=""
for (var i=0;i<a.length;i++)
{
for (var j=0;j<b.length;j++)
{ if(a[i]==b[j]&&c.indexOf(a[i])<0)
c+=a[i]+","
}
}
'
declare @ object int
declare @ r varchar (800 )
exec sp_OACreate 'MSScriptControl.ScriptControl' , @ object output
exec sp_OASetProperty @ object , 'Language' , 'javascript'
exec sp_OAMethod @ object , 'eval' , @ r out , @ str
return @ r
end
go
select dbo . fn_getArray ('abc,dced,f23e,gew432' , '23c,abc,f23e,geafd,ged' )
/* 输出
abc,f23e,
*/
复制代码 代码如下:
use tempdb
go
if (object_id ('fn_getArray' ) is not null )
drop function dbo . fn_getArray
go
create function fn_getArray (@ inStr1 varchar (8000 ), @ inStr2 varchar (8000 ))
returns varchar (8000 )
as
begin
declare @ str varchar (8000 )
set @ str = '
var a = [''' + replace (@ inStr1 , ',' , ''',''' )+ '''];
var b = [''' + replace (@ inStr2 , ',' , ''',''' )+ '''];
var c=""
for (var i=0;i<a.length;i++)
{
for (var j=0;j<b.length;j++)
{ if(a[i]==b[j]&&c.indexOf(a[i])<0)
c+=a[i]+","
}
}
'
declare @ object int
declare @ r varchar (800 )
exec sp_OACreate 'MSScriptControl.ScriptControl' , @ object output
exec sp_OASetProperty @ object , 'Language' , 'javascript'
exec sp_OAMethod @ object , 'eval' , @ r out , @ str
return @ r
end
go
select dbo . fn_getArray ('abc,dced,f23e,gew432' , '23c,abc,f23e,geafd,ged' )
/* 输出
abc,f23e,
*/
栏 目:MsSql
下一篇:通过系统数据库获取用户所有数据库中的视图、表、存储过程
本文标题:SQLServer中求两个字符串的交集
本文地址:http://www.codeinn.net/misctech/172086.html