0

Search a table record that contains and or starts with wildcard character % using the below query works. But how to pass % from C# as string and query the postgres table that contains and or starts with %.

Starts with Query select * from somepostgrestable where table_column like '%%' --ESCAPE '' limit 5;

Ramaswamy
  • 79
  • 1
  • 8
  • Does this answer your question? [How can you find a literal percent sign (%) in PostgreSQL using a LIKE query?](https://stackoverflow.com/questions/33007164/how-can-you-find-a-literal-percent-sign-in-postgresql-using-a-like-query) – madreflection Dec 01 '22 at 16:55
  • Why is your `ESCAPE` clause commented out? – madreflection Dec 01 '22 at 16:56
  • @madreflection In postgres the query is fine but how do you pass it from C# ???? And I commnet out ESCAPE to make sure if it works and it does – Ramaswamy Dec 01 '22 at 17:00
  • Microsoft's [documentation for strings](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/strings/#declaring-and-initializing-strings) shows you how to do that. – madreflection Dec 01 '22 at 17:02
  • 1
    Should that not be `select * from somepostgrestable where table_column like '%\%%' limit 5` per [Like](https://www.postgresql.org/docs/current/functions-matching.html#FUNCTIONS-LIKE): *To match a literal underscore or percent sign without matching other characters, the respective character in pattern must be preceded by the escape character. The default escape character is the backslash but a different one can be selected by using the ESCAPE clause. To match the escape character itself, write two escape characters.* – Adrian Klaver Dec 01 '22 at 17:05
  • https://stackoverflow.com/questions/18532691/how-do-i-write-a-backslash-in-a-string#:~:text=If%20you%20want%20to%20include,of%20the%20verbatim%20string%20literal. – madreflection Dec 01 '22 at 17:08
  • Avoid having to Escape altogether with a regular expression where `%` is not a wildcard. `select * from table where col ~ '^%'`. Then you should able to just pass % as any other character. – Belayer Dec 01 '22 at 18:47

0 Answers0