How to write SQL query to find a word in string"

  • Hi,

    I am trying to write a SQL statement to find related videos in a video site for a particular video. The TAG terms under a video goes as a query to this SQL statement to find related videos. Following SQL statement returns only the same video (single video) that has the exact tags. How do I change this to find all videos that has any word of the TAG string. Thanks. Norman

    SELECT TOP 20 * FROM Videos WHERE isEnabled=1 AND isPrivate=0

    AND title like '%Music Video Classic 80s %'

    OR description like '%Music Video Classic 80s %'

    OR tags like '%Music Video Classic 80s %'

    ORDER BY date_added DESC

  • hello try this procedure maybe it helps you ...Cheers

    CREATE PROCEDURE SEARCH

    @SEARCH NVARCHAR(200)

    AS

    SELECT TOP 20 * FROM VIDEOS

    WHERE isEnabled=1 AND isPrivate=0

    AND (TITLE LIKE ('%' + @SEARCH% + '%') OR

    DESCRIPTION LIKE ('%' + @SEARCH+ '%') OR

    TAGS LIKE ('%' + @SEARCH+ '%')

    );

    GO

    And you can try search like this:

    EXEC SEARCH

    BLLA BLLA BLLA

    ============================================================
    SELECT YOUR PROBLEM FROM SSC.com WHERE PROBLEM DESCRIPTION =
    http://www.sqlservercentral.com/articles/Best+Practices/61537/[/url]

  • Hi Dugi, thanks for the quick reply. Here is the vb code that do this search. Isn't that same as yours?

    Public Function Search_Related_Videos(ByVal search As String) As DataSet

    Dim Query As String = "SELECT TOP 20 * FROM Videos WHERE isEnabled=1 AND isPrivate=0"

    If Not search = "all" Then

    Query = Query & " AND"

    End If

    If Not search = "all" Then

    Query = Query & " (title like '%" & search & "%'"

    Query = Query & " OR description like '%" & search & "%'"

    Query = Query & " OR tags like '%" & search & "%')"

    End If

    '// Order by clause

    Query = Query & " ORDER BY date_added DESC"

    Return SqlHelper.ExecuteDataset(_constring, CommandType.Text, Query)

    End Function

  • Check your exectution plan on this one and watch your indexes. Using LIKE with a leading wildcard will result in a table scan if you're not careful. From a high level glance, an index on isEnabled/isPrivate may help things here. It's hard to say not knowing the selectivity of those columns, your table row count, etc. Bottom line is, check your execution plan as this could lead to being a poor performer.

    John Rowan

    ======================================================
    ======================================================
    Forum Etiquette: How to post data/code on a forum to get the best help[/url] - by Jeff Moden

  • ng949 (3/20/2008)


    Hi,

    I am trying to write a SQL statement to find related videos in a video site for a particular video. The TAG terms under a video goes as a query to this SQL statement to find related videos. Following SQL statement returns only the same video (single video) that has the exact tags. How do I change this to find all videos that has any word of the TAG string. Thanks. Norman

    SELECT TOP 20 * FROM Videos WHERE isEnabled=1 AND isPrivate=0

    AND title like '%Music Video Classic 80s %'

    OR description like '%Music Video Classic 80s %'

    OR tags like '%Music Video Classic 80s %'

    ORDER BY date_added DESC

    You should SPLIT the string and it will looks like:

    SELECT TOP 20 * FROM Videos WHERE isEnabled=1 AND isPrivate=0

    AND title like '%Music Video Classic 80s %'

    OR description like '%Music Video Classic 80s %'

    OR tags like '%Music%'

    OR tags like '%Video%'

    OR tags like '%Classic%'

    OR tags like '%80s%'

    ORDER BY date_added DESC

    I think you can get my idea.

    Cheers,

    Gonzalo

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply