0

I want to find string that have % character

For example, when one row's column has %%%%%,

SELECT * FROM table WHERE column LIKE '%value%';

If value is %, it returns all.

How can I search only that row?

Darren
  • 13,050
  • 4
  • 41
  • 79
  • This link might be helpful.. [Like query](http://stackoverflow.com/questions/712580/list-of-special-characters-for-sql-like-clause) – FaNtAMode Nov 13 '15 at 05:57
  • 3
    Possible duplicate of [How to escape literal percent sign when NO\_BACKSLASH\_ESCAPES option is enabled?](http://stackoverflow.com/questions/5020130/how-to-escape-literal-percent-sign-when-no-backslash-escapes-option-is-enabled) – Keale Nov 13 '15 at 06:09

4 Answers4

1

You should escape it with a backslash...

\%
Darren
  • 13,050
  • 4
  • 41
  • 79
Jason Byrne
  • 1,579
  • 9
  • 19
1

Or you could use any escape character with:

LIKE '%|%%' escape '|'
jitendrapurohit
  • 9,435
  • 2
  • 28
  • 39
0

You can use something like this to escape all % characters in PHP:

$original_value = "%%%%%";

$temp = str_split($original_value, 1);

$escape_value = '\\'.implode('\\', $temp);

echo $escape_value;

Result (escaped value):

\%\%\%\%\%
Kostas Mitsarakis
  • 4,772
  • 3
  • 23
  • 37
0

Let’s say you want to find any fields that contain the text “100%”, so you put together this query:

Square Bracket Escape:

You can surround the % or _ with square brackets to tell SQL Server that the character inside is a regular character.

SELECT * FROM tablename WHERE fieldname LIKE ‘%100[%]%’

T-SQL ESCAPE Syntax:

Alternatively, you can append the ESCAPE operator onto your query, and add a \ character before the value you want to escape.

SELECT * FROM tablename WHERE fieldname LIKE ‘%100\%%’ ESCAPE ‘\’

The ESCAPE ‘\’ part of the query tells the SQL engine to interpret the character after the \ as a literal character instead of as a wildcard.

The easiest way is to include the characters I don't want, for example square brackets and slash:

SELECT * FROM Table
WHERE Name LIKE '%[\]\[/]%' ESCAPE '\'
Santosh
  • 393
  • 2
  • 11