Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you find a literal percent sign (%) in PostgreSQL using a LIKE query?

Tags:

I have character varying entries in a table where some (not all) values contain percentages, e.g., '12%', '97%', etc. I want to find all the values that contain percentages. In other words, I want to find all values that end with a percent sign ('%').

like image 521
magnus Avatar asked Oct 08 '15 05:10

magnus


People also ask

How do I show percentage in PostgreSQL?

We have several options if we want to display numbers with a percentage sign in PostgreSQL. We can use the TO_CHAR() function to format the number along with the percentage sign. Or we can simply concatenate the number with the percentage sign, either with the CONCAT() function or with the concatenation operator.

How do I use like keyword in PostgreSQL?

The PostgreSQL LIKE operator is used to match text values against a pattern using wildcards. If the search expression can be matched to the pattern expression, the LIKE operator will return true, which is 1. The percent sign represents zero, one, or multiple numbers or characters.

How do I find special characters in PostgreSQL?

SELECT * FROM spatial_ref_sys WHERE srtext LIKE '%\ /%'; Sometimes these ticks are very useful for searching special characters in a database.

What does percentage sign mean in SQL query?

The SQL LIKE Operator The percent sign (%) represents zero, one, or multiple characters. The underscore sign (_) represents one, single character.

What are literals in PostgreSQL?

In PostgreSQL, a literal is the same as a constant. We'll cover several types of literals - string literals, number literals, date and time literals and boolean literals. String literals are always surrounded by single quotes ('). For example: Number literals can be either positive or negative numbers that are exact or floating point values.

How to use like query in PostgreSQL?

Postgres, like the query, are basically used to match the text values from the pattern we have used in the query example; we can match the text values using the wildcards operator. If the search expression matches the given condition, which we have used with like query, then like query will return the true values, the true value is considered one.

What does the percent sign mean in JavaScript?

The percent sign (%) is used to represent zero, one, or many characters or numbers. The underscore wildcard (_) is used to represent one character or number. These symbols can also be combined.

What does the percent sign mean in LIKE operator?

If the search expression can be matched to the pattern expression, the LIKE operator will return true, which is 1. The percent sign represents zero, one, or multiple numbers or characters. The underscore represents a single number or character.


2 Answers

You can try like this:

SELECT * FROM my_table  WHERE my_column LIKE '%\%%' ESCAPE '\'; 

Format

 <like predicate> ::=       <match value> [ NOT ] LIKE <pattern>         [ ESCAPE <escape character> ]   <match value> ::= <character value expression>   <pattern> ::= <character value expression>   <escape character> ::= <character value expression> 
like image 138
Rahul Tripathi Avatar answered Sep 19 '22 16:09

Rahul Tripathi


You have to escape the literal % sign. By default the escape character is the backslash:

SELECT * FROM my_table  WHERE my_column LIKE '%\%'; 

In this case the first % sign matches any starting sequence in my_column. The remaining \% are interpreted as a literal % character. The combination is therefore: match anything that ends in a % character.

SQLFiddle

like image 45
Patrick Avatar answered Sep 20 '22 16:09

Patrick