Reputation: 5417
I have a WinForms C# app which uses a SqlCommand to extract info from a back end SqlServer DB. When a user types in a name, I want to inspect my DB table to look for similar names (say having the same two initial characters). I've been trying the following sql command text:
SELECT Name
FROM tblCases
WHERE Name LIKE SUBSTRING(@Name,1,2);
Where @Name is a parameter in the SqlCommand generated by the application, the value of which is set by the user.
But this returns nil query results every time even when I know it should be returning some results.
What I am trying to achieve is that is the user types say name "Smith", I want the DB to return values like 'Smythe' or 'Smithson', but I cant see to get the SQL command text right.
Help most appreciated.
Upvotes: 0
Views: 72
Reputation: 247860
You can do it this way with the LIKE
clause. It adds the wildcard %
at the end of you name you are passing:
SELECT Name
FROM tblCases
WHERE Name LIKE (SUBSTRING(@name,1,2) + '%')
Upvotes: 1