Search code examples
c#sqlsql-serverwildcardsql-like

C# and SQL LIKE condition: '%' works as single-character wildcard


I have query like that in my DataSet (database is located in .mdf file):

SELECT *
FROM TableName
WHERE SomeField LIKE @Param

Table TableName contains record Значение in SomeField field.

When @Param is Значени% it's works perfectly, but when @Param is Значен% or Значе%, it's returns 0 rows. Значе%%% also works.

Why '%' works as single-character wildcard?


Solution

  • Your problem is that you should be using @param of NVARCHAR, not NCHAR

    declare @Param nchar(255)
    set @Param = N'Значе%'
    

    This is really

    N'Значе%             ...' (many more spaces)
    

    So it won't match your data, which is

    N'Значение             ...' (padded with spaces)