2

I want to do select query with input from user, what is the correct syntax of LIKE to use, where I should set % in the first line or second?

var areaName = "SELECT AOIId FROM dbo.AreaOFInterest WHERE AOIName LIKE @AOIName ";
db.Database.ExecuteSqlCommand(areaName, @searching);
2
  • This is about your programming language, where you're trying to invoke this, not about sql. Commented Feb 17, 2018 at 15:34
  • I'm use asp.net mvc c# Commented Feb 17, 2018 at 16:10

1 Answer 1

1

If you want to search for all names starting with the specified characters, specify LIKE @AOIName + '%'. For all name containing the string, specify LIKE '%' @AOIName + '%'.

You may also want to escape LIKE wildcards within the provided search string so that these are treated as literals:

var aoiNameParameterValue = AOIName.Replace("[", "[[]").Replace("%", "[%]").Replace("_", "[_]").Replace("^", "[^]");
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks but what you mean by replace?
I mean enclose wildcard characters within the search string in square brackets. I used the String.Replace method in my code example to do that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.