mjyazdani
mjyazdani

Reputation: 2035

how to use more than one string in like expression in sql

I have a list of 20 (or more) words (string) and want to select the rows that have these word in 3 of their column. I should use like expression of sql. but I don't know how to use more than one string in like expression. (I do it with union now, but I have at least 60 select statement and think it reduced the performance, Is it really reduce the performance?)

        //get the advertise that have similar keywords
        foreach (string str in keywords)
        {
            if (str != "")
            {
                if (!string.IsNullOrEmpty(sqlQuery)) sqlQuery += " union";
                sqlQuery = "select * from AD_Advertise where (AdKeyWords like N'%" + str + "%'"
                    + " OR AdTitle like N'%" + str + "%' "
                    + " OR AdDescription like N'%" + str + "%' "
                    + " OR AdGroupItemCode=" + adinfo.AdGroupItemCode + ")"
                    + " AND AdSiteID=" + CMSContext.CurrentSiteID
                    + " AND AdShow='True' "
                    + " AND ItemID != " + ADId;
            }
        }

        ds = cn.ExecuteQuery(sqlQuery,null);//("AD.Advertise.selectall", null, where, "ItemModifiedWhen");

Answer:

At last I used below code:

 if object_id('tempdb..#WordList') is not null
    drop table #WordList
 CREATE TABLE #WordList ( KeyWord nvarchar(100))
 insert into #WordList values (N'حقوقی'),(N'وکیل');

 SELECT DISTINCT *
 FROM AD_ADvertise a
     LEFT JOIN #WordList k 
     ON a.AdKeywords LIKE '%' + k.KeyWord + '%' 
     OR  a.AdTitle LIKE '%' + k.KeyWord + '%' 
     OR a.AdDescription LIKE '%' + k.KeyWord + '%'
 WHERE
    (k.KeyWord IS NOT NULL OR a.AdGroupItemCode = @AdGroupItemCode)
    AND a.AdSiteId = @AdSiteId
    AND a.AdShow = 'True'
    AND a.ItemId != @ItemId
;drop table #WordList

Upvotes: 0

Views: 966

Answers (3)

James S
James S

Reputation: 3588

Create a stored procedure with a table valued parameter that takes your list of strings.

Have a join between the table valued parameter and your tables AD_Advertise on the like.

Heres how to do the Table type + the stored procedure:

CREATE TYPE WordList AS TABLE (Word NVARCHAR(50));
GO

CREATE PROCEDURE GetAddsMatchingKeywords
    @KeywordList WordList READONLY,
    @AdGroupItemCode VARCHAR(50),
    @AdSiteId INT,
    @ItemId INT
AS
    SELECT DISTINCT
        a.AdTitle, 
        a.ItemId -- extend to the full column list
    FROM AD_ADvertise a
        LEFT JOIN @KeywordList k ON a.AdKeywords LIKE '%' + k.Word + '%' OR  a.AdTitle LIKE '%' + k.Word + '%' OR a.AdDescription LIKE '%' + k.Word + '%'
    WHERE
        (k.Word IS NOT NULL OR a.AdGroupItemCode = @AdGroupItemCode)
        AND a.AdSiteId = @AdSiteId
        AND a.AdShow = 'True'
        AND a.ItemId = @ItemId

GO

edit - misread the original question - thought you wanted to match 3 or more words. This version matches any that have a single word in any of the 3 columns - like you wanted I think.

Upvotes: 1

podiluska
podiluska

Reputation: 51514

  1. Don't ever build up a SQL string like that. Read about SQL injection before you go any further.
  2. Ideally, you would have a table of keywords, rather than a string, and a join table to link between the item and the keywords.
  3. If you persist in doing it this way, have a look at Full Text Search

Upvotes: 1

dang
dang

Reputation: 193

    //get the advertise that have similar keywords
    foreach (string str in keywords)
    {
        if (str != "")
        {
            if (!string.IsNullOrEmpty(sqlQuery)) sqlQuery += " union";
            sqlQuery = "select * from AD_Advertise where (AdKeyWords + AdTitle + AdDescription like N'%" + str + "%'"
                + " OR AdGroupItemCode=" + adinfo.AdGroupItemCode + ")"
                + " AND AdSiteID=" + CMSContext.CurrentSiteID
                + " AND AdShow='True' "
                + " AND ItemID != " + ADId;
        }
    }

    ds = cn.ExecuteQuery(sqlQuery,null);//("AD.Advertise.selectall", null, where, "ItemModifiedWhen");

Upvotes: 0

Related Questions