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
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Creating wildcard searches

Author  Topic 

Bex
Aged Yak Warrior

580 Posts

Posted - 2006-02-21 : 11:47:25
I have a stored procedure that uses dynamic SQL to build a paramatized query based on varied input parameters. Rather than use a wildcard search on either sides of the field 'accountnumber' (as it would render the indexes useless) I am thinking of allowing the user to enter an * when they wish to perform such a search. Therefore, if an * is not contained in the string, then I can build a query that will make best use of indexes. It will look something like this:



IF @accountNumber IS NOT NULL
BEGIN
IF charindex('*',@accountNumber) = 0
BEGIN
SET @sql = 'SELECT * FROM Account WHERE (AccountNumber = ''' + @accountNumber + ''')'
END
ELSE IF charindex('*',@accountNumber) = 1
BEGIN
SET @sql = 'SELECT * FROM Account WHERE (AccountNumber LIKE ''' + '%' + REPLACE(@accountNumber,'*','') + ''')'
END
ELSE
BEGIN
SET @sql = 'SELECT * FROM Account WHERE (AccountNumber LIKE ''' + REPLACE(@accountNumber,'*','') + '%' + ''')'
END
END


Are there any better ways to achieve the same result, or different more effective methods?

Thankyou

Hearty head pats

X002548
Not Just a Number

15586 Posts

Posted - 2006-02-21 : 12:32:32
You could give this a read

http://weblogs.sqlteam.com/brettk/archive/2004/05/05/1312.aspx



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam
Go to Top of Page

Bex
Aged Yak Warrior

580 Posts

Posted - 2006-02-22 : 04:12:11
Thanks Brett

I shall have a read of that this morning. Its amazing how different ways there are to approach such a query. But its all part of the learning curve.

Hearty head pats
Go to Top of Page
   

- Advertisement -