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
 SQL Search String Function

Author  Topic 

WelshPunk
Yak Posting Veteran

67 Posts

Posted - 2008-06-02 : 02:02:33
The 'LIKE' function looks for words that start with whatever is in the like condition. Is there an sql function similar but will look and compare at any part of the search string.

For example I am using a webservice in dot net to populate a dropdown list using this sql

SELECT compound_name FROM dbo.compound_name WHERE compound_name like @prefixText

In this table there is a compound called SILCAP310 and I would like the search function to pick up 310 if I put this into the @prefix parameter. (but I would still like the search to perform like the 'LIKE' does also.

SELECT compound_name FROM dbo.compound_name WHERE compound_name like @prefixText or compound_name SearchPartString @prefixText

Thanks in advance

ranganath
Posting Yak Master

209 Posts

Posted - 2008-06-02 : 02:30:30



SELECT compound_name FROM dbo.compound_name WHERE compound_name like '%' +@prefixText + '%'
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-02 : 02:36:16
May be this:-
SELECT compound_name FROM dbo.compound_name WHERE compound_name LIKE '%' + @prefixText + '%'
Go to Top of Page

WelshPunk
Yak Posting Veteran

67 Posts

Posted - 2008-06-02 : 02:37:19
Thank you both. That sorts it out
Go to Top of Page

blindman
Master Smack Fu Yak Hacker

2365 Posts

Posted - 2008-06-02 : 09:44:33
I think you should use visakh's suggestion.
While ranganath's code is more concise, being one character shorter, visakh's consistent use of white space improves readability and will make debugging easier.
Nice try, though, ranganath. Good effort. ;)

e4 d5 xd5 Nf6
Go to Top of Page
   

- Advertisement -