显示成功的模式作为结果

时间:2014-02-12 13:42:35

标签: sql-server pattern-matching

我正在做这样的模式匹配:

SELECT     ea.Text
    FROM         dbo.Parameters AS n INNER JOIN
                          dbo.Descriptions AS ea ON ea.Text LIKE '%' + n.par1 + n.par2 + n.Achtervoegsel + '%' OR 
                          ea.Text LIKE '%' + n.par1 + '_' + n.par2 + '_' + n.par3 + '%' OR 
                          ea.Text LIKE '%' + n.par1 + n.par2 + '_' + n.par3 + '%' OR ea.Text LIKE '%' + n.par1 + '_' + n.par2 + n.par3 + '%' OR 
                          ea.Text LIKE '%' + n.par1 + '__' + n.par2 + n.par3 + '%' OR 
                          ea.Text LIKE '%' + n.par1 + '__' + n.par2 + '_' + n.par3 + '%'

嗯,这很好用。六种可能的模式正在匹配。 但现在我的问题。

我想添加一个输出列,向我展示成功的模式。 例如,如果我的ea.Text是:

这是APPLE 2000-A和其他事项的长篇描述

和我的参数表有一个记录 par1'APPLE' par2'2000' par3'A'

然后这个模式会得到一个结果:

'%' + n.par1 + '_' + n.par2 + '_' + n.par3 + '%'

在这种情况下,我希望有一个列

APPLE 2000-A

这可能吗?

2 个答案:

答案 0 :(得分:0)

如果您知道幕后的“OR”比使用工会的返工查询,您可以执行以下操作:

SELECT ea.Text, n.par1 + n.par2 + n.Achtervoegsel
FROM dbo.Parameters AS n 
  INNER JOIN dbo.Descriptions AS ea ON ea.Text LIKE '%' + n.par1 + n.par2 + n.Achtervoegsel + '%'
UNION ALL
SELECT ea.Text, n.par1 + '_' + n.par2 + '_' + n.par3
FROM dbo.Parameters AS n 
  INNER JOIN dbo.Descriptions AS ea ON ea.Text LIKE '%' + n.par1 + '_' + n.par2 + '_' + n.par3 + '%'
UNION ALL
SELECT ea.Text, n.par1 + n.par2 + '_' + n.par3
FROM dbo.Parameters AS n 
  INNER JOIN dbo.Descriptions AS ea ON ea.Text LIKE '%' + n.par1 + n.par2 + '_' + n.par3 + '%'
UNION ALL
SELECT ea.Text, n.par1 + '_' + n.par2 + n.par3
FROM dbo.Parameters AS n 
  INNER JOIN dbo.Descriptions AS ea ON ea.Text LIKE '%' + n.par1 + '_' + n.par2 + n.par3 + '%'
UNION ALL
SELECT ea.Text, n.par1 + '__' + n.par2 + n.par3
FROM dbo.Parameters AS n 
  INNER JOIN dbo.Descriptions AS ea ON ea.Text LIKE '%' + n.par1 + '__' + n.par2 + n.par3 + '%'
UNION ALL
SELECT ea.Text, n.par1 + '__' + n.par2 + '_' + n.par3
FROM dbo.Parameters AS n 
  INNER JOIN dbo.Descriptions AS ea ON ea.Text LIKE '%' + n.par1 + '__' + n.par2 + '_' + n.par3 + '%'

如果你真的想要匹配的确切值(而不是'_'-s),你可能想要查看substring()函数。我不知道该特定问题的任何其他解决方案。

答案 1 :(得分:0)

如果你可以创建一个函数,一个很好的方法就是在那里进行匹配,并返回你找到的搜索字符串!然后,您可以轻松地将匹配的字符串作为列返回:

功能

CREATE FUNCTION dbo.MatchingString
(
    @text nvarchar(100),
    @par1 nvarchar(100),
    @par2 nvarchar(100),
    @par3 nvarchar(100)
)
RETURNS nvarchar(100)
AS
BEGIN

    declare @searchString nvarchar(100);
    declare @currSearchString nvarchar(100);
    declare @charIndex int = -1;

    set @currSearchString = Concat(@par1,'_',@par2,'_',@par3);
    set @charIndex = CharIndex(@searchString,@text,0);

    if(@charIndex >= 0) begin
        set @searchString = @currSearchString;
    end

    --loop over more searchstrings or do it manually


    return @searchString;
END

查询

    SELECT     ea.Text,
MatchingString = dbo.MatchingString(ea.Text,n.par1,n.par2,n.par3)
    FROM         dbo.Parameters AS n INNER JOIN
        dbo.Descriptions AS ea ON dbo.MatchingString(ea.Text,n.par1,n.par2,n.par3) IS NOT NULL