Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Writing SQL searchs using LIKE and @paramaters

Author  Topic 

shawnmolloy
Yak Posting Veteran

93 Posts

Posted - 2007-01-17 : 16:24:09
I'm writing a search query in TSQL for MS SQL. Here is my code:


SELECT a.*, b.username, c.categoryname FROM   
video a
inner join
user b
on
a.userid = b.userid
inner join
video_category c
on
a.categoryId = c.categoryid
where
a.videotitle like '%@SearchText%'
OR
a.description like '%@SearchText%'



This returns no results, but wheN I replace the '%@SearchText%' with my search text inplace of the @SearchText it returns retuls. I have a feelign I am passing the params to the LIKE clause incorrectly. Any suggestions?

Thanks,

-- shawn

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2007-01-17 : 16:31:51
LIKE '%' + @SearchText + '%'

Tara Kizer
Go to Top of Page

shawnmolloy
Yak Posting Veteran

93 Posts

Posted - 2007-01-17 : 17:26:30
Thanks. I added your suggestions and it gives me this error:

Msg 402, Level 16, State 1, Procedure sp_video_getSearchResults, Line 8
The data types varchar and text are incompatible in the add operator.
Go to Top of Page

agossage
Starting Member

9 Posts

Posted - 2007-01-17 : 17:33:13
You will need to convert the @SearchText to varchar(MAX).

Adam Gossage
Lake Wylie, SC, USA
Go to Top of Page

shawnmolloy
Yak Posting Veteran

93 Posts

Posted - 2007-01-17 : 17:37:31
Yup that worked! I was using text as the datatype.

Thanks.
Go to Top of Page

shawnmolloy
Yak Posting Veteran

93 Posts

Posted - 2007-01-17 : 17:37:31
Yup that worked! I was using text as the datatype.

Thanks.
Go to Top of Page
   

- Advertisement -