Questions tagged [sql-like]

The LIKE predicate is used to search for a specific pattern in a column.

The LIKE predicate is used to search for a specified pattern in a column. The wildcard character % stands for a sequence of zero or more characters, the wildcard character _ stands for a single character.

Examples of usage:

Starts with P

SELECT * FROM Person
WHERE name LIKE 'P%'

Ends with P

WHERE name LIKE '%P'

Contains P (will match anywhere in the string so this potentially returns a lot of rows)

WHERE name LIKE '%P%'

Has an E as second character:

WHERE name LIKE '_E%'

You can also combine any other conditions using AND or OR operators.

References

3176 questions
47
votes
3 answers

Performance of like '%Query%' vs full text search CONTAINS query

I have a situation where I would like to search a single word. For that scenario, which query would be good from a performance point of view? Select Col1, Col2 from Table Where Col1 Like '%Search%' or Select Col1, Col2 from Table Where Col1…
dotnetguts
  • 735
  • 3
  • 7
  • 8
46
votes
5 answers

SQL Not Like Statement not working

I have the following code within a stored procedure. WHERE WPP.ACCEPTED = 1 AND WPI.EMAIL LIKE '%@MATH.UCLA.EDU%' AND (WPP.SPEAKER = 0 OR WPP.SPEAKER IS NULL) AND WPP.COMMENT NOT LIKE '%CORE%' AND WPP.PROGRAMCODE =…
mattgcon
  • 4,768
  • 19
  • 69
  • 117
46
votes
5 answers

implement LIKE query in PDO

I am running problems in implementing LIKE in PDO I have this query: $query = "SELECT * FROM tbl WHERE address LIKE '%?%' OR address LIKE '%?%'"; $params = array($var1, $var2); $stmt = $handle->prepare($query); $stmt->execute($params); I checked…
Leandro Garcia
  • 3,138
  • 11
  • 32
  • 44
43
votes
4 answers

Which is faster — INSTR or LIKE?

If your goal is to test if a string exists in a MySQL column (of type 'varchar', 'text', 'blob', etc) which of the following is faster / more efficient / better to use, and why? Or, is there some other method that tops either of these? INSTR(…
Grekker
  • 944
  • 1
  • 9
  • 17
41
votes
8 answers

SQL 'LIKE' query using '%' where the search criteria contains '%'

I have an SQL query as below. Select * from table where name like '%' + search_criteria + '%' If search_criteria = 'abc', it will return data containing xxxabcxxxx which is fine. But if my search_criteria = 'abc%', it will still return data…
pratik
  • 4,419
  • 8
  • 41
  • 65
40
votes
8 answers

Like Case Sensitive in MySQL

I have a MySQL query: SELECT concat_ws(title,description) as concatenated HAVING concatenated LIKE '%SearchTerm%'; And my table is encoded utf8_general_ci with MyISAM. Searches seem to be case sensitive. I can't figure out how to fix it. What's…
Pmc Machinery
  • 450
  • 2
  • 5
  • 8
39
votes
12 answers

MySQL - How to search for exact word match using LIKE?

I'm using this query to select data: mysql_query("SELECT * FROM products WHERE product_name LIKE '%".$search."%'"); The only problem is, that it sometimes selects more, than I would like. For example, I would like to select product "BLA", but my…
Mike
  • 6,854
  • 17
  • 53
  • 68
39
votes
3 answers

Pandas text matching like SQL's LIKE?

Is there a way to do something similar to SQL's LIKE syntax on a pandas text DataFrame column, such that it returns a list of indices, or a list of booleans that can be used for indexing the dataframe? For example, I would like to be able to match…
naught101
  • 18,687
  • 19
  • 90
  • 138
38
votes
4 answers

MySQL like another field

I have a table with two string columns: Url and ModelId. I need to return records for which Url contains ModelId, something like this: SELECT Id, Url, ModelId WHERE Url like "%ModelId%"
SiberianGuy
  • 24,674
  • 56
  • 152
  • 266
38
votes
3 answers

How do I find ' % ' with the LIKE operator in SQL Server?

I have a column name Address which consists of some address which has '%' in between as: Address -------------------- Aman Ja%lan% Stree% Ro%ad etc., etc. How I can write the LIKE operator to find that pattern? I tried: declare @var char(1) set…
Teju MB
  • 1,333
  • 5
  • 20
  • 37
38
votes
2 answers

SQL Join on a column LIKE another column

Possible Duplicate: mysql join query using like? I want to do a join where one column contains a string from another table's column: SELECT a.first_name, b.age FROM names a JOIN ages b ON b.full_name LIKE '%a.first_name%' Is this possible? I'm…
Don P
  • 60,113
  • 114
  • 300
  • 432
36
votes
7 answers

Cannot use a LIKE query in a JDBC PreparedStatement?

The query code and query: ps = conn.prepareStatement("select instance_id, ? from eam_measurement where resource_id in (select RESOURCE_ID from eam_res_grp_res_map where resource_group_id = ?) and DSN like '?' order by…
SeerUK
  • 497
  • 2
  • 5
  • 10
36
votes
3 answers

How do I perform a case-sensitive search using LIKE?

I'm trying to find records that contain a string of 6 or more alpha-numeric characters in uppercase. Some examples: PENDING 3RDPARTY CODE27 I'm using the following statement: SELECT Details FROM MyTable WHERE Details LIKE…
Hand-E-Food
  • 12,368
  • 8
  • 45
  • 80
35
votes
5 answers

Mysql: Order by like?

assume that we are performing search using keywords: keyword1, keyword2, keyword3 there are records in database with column "name": 1: John Doe 2: Samuel Doe 3: John Smith 4: Anna Smith now Query: SELECT * FROM users WHERE (name LIKE "%John%" OR…
dfens
  • 5,413
  • 4
  • 35
  • 50
34
votes
3 answers

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

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…
magnus
  • 4,031
  • 7
  • 26
  • 48