SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


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


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

Author
Message
loopman
loopman
Grasshopper
Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)

Group: General Forum Members
Points: 22 Visits: 32
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
Dugi
Dugi
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2126 Visits: 3511
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/
loopman
loopman
Grasshopper
Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)

Group: General Forum Members
Points: 22 Visits: 32
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
John Rowan
John Rowan
SSCertifiable
SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)

Group: General Forum Members
Points: 5984 Visits: 4547
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 - by Jeff Moden
Gonzalo-603688
Gonzalo-603688
SSC Journeyman
SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)SSC Journeyman (84 reputation)

Group: General Forum Members
Points: 84 Visits: 31
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search