Marco Dinatsoli
Marco Dinatsoli

Reputation: 10570

Executing query with like is not working

I have this code:

string query = "SELECT * FROM dbo.customer WHERE mobileNumber Like '%@mobileNumber'";

When I execute that code from c#, I got empty results

However when I execute this query in my sql managmenet studio

SELECT * FROM dbo.customer WHERE mobileNumber Like '%454545'

I got result.

What I am missing?

Upvotes: 1

Views: 77

Answers (2)

Randy Minder
Randy Minder

Reputation: 48392

Try this:

string query = "SELECT * FROM dbo.customer WHERE mobileNumber Like '%' + @mobileNumber

This is assuming @mobileNumber is a parameter containing the value for which you want to search.

Upvotes: 4

Patrick Hofman
Patrick Hofman

Reputation: 156918

It seems from your code that @mobileNumber is a parameter. Yet, you place it here as a string.

Try putting the % in a string and the parameter separately unquoted:

string query = "SELECT * FROM dbo.customer WHERE mobileNumber Like '%' + @mobileNumber";

Upvotes: 2

Related Questions