SQL Wild Card Search

Hello Experts,


Is this something possible in SQL?

Select FirstName from Authors Where FirstName like 'MANNY%'

Right now i am getting:

MANNY
MANNYXXX


but what i want is when the user types 'MANNY"

I want the query to pull the letters starting with

MANNYXXX, MANNY, MAN, MA, M etc....


Thanks





LVL 8
mani_saiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Aneesh RetnakaranDatabase AdministratorCommented:
declare @x   varchar(10)
declare @len int
declare @SQL nvarchar(4000)


SET @x = 'MANNY'
SET @len = LEN(@x)
SET @sql =''

WHILE @len >0
BEGIN
   SELECT @sql =@sql +  ' Select FirstName from Authors Where FirstName like '''+ LEFT(@x,@len)+'%'''
   SELECT @len = @Len -1
   IF @len > 0
        SET @sql = @sql + '  UNION '
END

print @sql
exec sp_executeSQL @Sql
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
adwisemanCommented:
If you want to return everyrecord that matches 'M%" then
Select *
from Authors
Where FirstName like 'M%'

But I think you getting at, you want to return all those matches, plus sort them in order of how close of a match they are.  This generates the dynamic SQL do do that

DECLARE @Filter VARCHAR(30)
SET @Filter = 'MANNY'
DECLARE @Script VARCHAR(4000)
DECLARE @Len INT

SET @Len = LEN(@Filter)

SET @Script = 'Select FirstName, CASE WHEN FirstName LIKE ''' + @Filter + '%'' THEN ' + CONVERT(VARCHAR, @Len) + CHAR(13)

WHILE @Len > 0
BEGIN
   SET @Len =@Len - 1

   SET @Script = @Script + 'WHEN FirstName LIKE ''' + LEFT(@Filter, @Len) + '%'' THEN ' + CONVERT(VARCHAR, @Len) + CHAR(13)
   
END

SET @Script = @Script + 'ELSE NULL END AS MyOrderField
from Authors
Where FirstName like ''' + LEFT(@Filter, 1) + '%''
ORDER BY MyOrderField desc'
print @script
exec(@script)
0
adwisemanCommented:
CHANGE 1 line above

WHILE @Len > 0
SHOULD BE
WHILE @Len > 1

That produces this SQL

Select FirstName, CASE WHEN FirstName LIKE 'MANNY%' THEN 5
WHEN FirstName LIKE 'MANN%' THEN 4
WHEN FirstName LIKE 'MAN%' THEN 3
WHEN FirstName LIKE 'MA%' THEN 2
WHEN FirstName LIKE 'M%' THEN 1
ELSE NULL END AS MyOrderField
from Authors
Where FirstName like 'M%'
ORDER BY MyOrderField desc
0
mani_saiAuthor Commented:
Thanks, It works.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.