Selecting data using % wildcard character

M

Maciek Pawlowski

I have designed an sql query to retrieve data from a table based on an
integer field which happens to be the primary key for the table.

The table looks like this:

tbl_players

Cust Id (int) <---Primary key
fName (varchar 50)
lName (varchar 50)

Now, I have the following SQL query to return the fName and lName according
to the CustId:

SELECT fName, lName
FROM tbl_players
WHERE custId LIKE @custId

The database is an SQL Server database.

Now, I want to be able to select all players from the table by passing in
the % wildcard character to the @custId parameter from my asp.net app using
C# codebehind. However, when I set the value of the @custId parameter to %,
nothing gets selected. If I pass in a valid custId (ie one that exists in
the database) it returns the appropriate data.

What am I doing wrong? I use the following line to assign the % character
to the @custId parameter:

sqlSelectCommand.Parameters["@custId].Value = '%';

I have also tried:

sqlSelectCommand.Parameters["@custId].Value = "%";

Thanks in advance,
maciek
 
W

William \(Bill\) Vaughn

Ah no. The LIKE operator is designed to work against strings--not integers.
While you could write an expression to convert the integer to a string in
the SP and then use the LIKE expression, I would suggest you use an
expression suitable for an integer such as
... WHERE custID = @CustID
or
... WHERE custID BETWEEN @CustIDLow AND @CustIDHigh

hth
--
____________________________________
Bill Vaughn
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top