Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to escape a % sign in sqlite?

Tags:

sqlite

I do a full text search using LIKE clause and the text can contain a '%'.

What is a good way to search for a % sign in an sqlite database?

I did try

SELECT * FROM table WHERE text_string LIKE '%[%]%' 

but that doesn't work in sqlite.

like image 545
DeepBlack Avatar asked Apr 27 '14 02:04

DeepBlack


People also ask

How do I escape a character in SQLite?

Double-quotes in SQLite identifiers are escaped as two double quotes. SQLite identifiers preserve case, but they are case-insensitive towards ASCII letters. It is possible to enable unicode-aware case-insensitivity.

How do you escape the symbol in SQL query?

In ANSI SQL, the backslash character (\) is the escape character. To search for data that begins with the string \abc , the WHERE clause must use an escape character as follows: ... where col1 = '\\abc';

What does || mean in SQLite?

Description. The SQLite || operator allows you to concatenate 2 or more strings together.

What does escaping mean in SQL?

Escape sequences are used within an SQL statement to tell the driver that the escaped part of the SQL string should be handled differently. When the JDBC driver processes the escaped part of an SQL string, it translates that part of the string into SQL code that SQL Server understands.


2 Answers

From the SQLite documentation

If the optional ESCAPE clause is present, then the expression following the ESCAPE keyword must evaluate to a string consisting of a single character. This character may be used in the LIKE pattern to include literal percent or underscore characters. The escape character followed by a percent symbol (%), underscore (_), or a second instance of the escape character itself matches a literal percent symbol, underscore, or a single escape character, respectively.

like image 95
aruisdante Avatar answered Oct 19 '22 07:10

aruisdante


We can achieve same thing with the below query

SELECT * FROM table WHERE instr(text_string, ?)>0

Here :

? => your search word

Example :

You can give text directly like

SELECT * FROM table WHERE instr(text_string, '%')>0

SELECT * FROM table WHERE instr(text_string, '98.9%')>0 etc.

Hope this helps better.

like image 3
shobhan Avatar answered Oct 19 '22 07:10

shobhan