Home > Articles > Details

SQL Query Like does not work with Spaces in a String

Question: How do you get the TSQL Like keyword to work with spaces in the String Value?


Please Login to see the rest of the answer
Answer:
You will have to replace the Space or Spaces in your String or Value passed to the SQL Server. See the example of C# (C-Sharp) code below:

 public async Task<List<YourClassModel>> SearchByMyValue(string myAwesomeValue)
        {
            
            string cleanedAwesomeValue = myAwesomeValue.Replace(" ","%");
//Then you can pass the cleanedAwesomeValue to the Database
}

//Sql Store Procedure
You can now use "LIKE" to search for cleanedAwesomeValue passed in as Store Proc Arguments

- In some other cases, you can just trim the values you are sending to the database by using a Trim() function built-in C-Sharp.
For example, myValueString.Trim() this will almost if at all give you the same results.


If you log in, you will be notified when someone leaves a comment.

Other users would like to know if this solution helped you.


© 2022 - ErnesTech - Privacy