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
24
votes
3 answers

How to retrieve rows that begin and end with specific characters?

I have one table called STUDENT. In this table there are two columns, ID and NAME. I want to retrieve all rows from this table where name starts with 'ab' and ends with 'k'. What is the SQL query for doing this?
jaleel
  • 1,169
  • 8
  • 22
  • 46
24
votes
9 answers

Using SQLServer contains for partial words

We are running many products search on a huge catalog with partially matched barcodes. We started with a simple like query select * from products where barcode like '%2345%' But that takes way too long since it requires a full table scan. We thought…
Guy Korland
  • 9,139
  • 14
  • 59
  • 106
24
votes
5 answers

"Like" operator in inner join in SQL

Using Sequel Pro, I have these two tables: Table1 Name Year x y John Smith 2010 10 12 Adam Jones 2010 8 13 John Smith 2011 7 15 Adam Jones 2011 9 14 etc. and Table2 Name …
japem
  • 1,037
  • 5
  • 16
  • 30
22
votes
4 answers

like and not like in one mysql query

I want to do a query containing 'like' and 'not like'. Current example: i want everything starting with '1|%' but not with '1|6|199|%' or '1|6|200|%'. Current query: 'SELECT * FROM `links` WHERE `category` LIKE '1|%' NOT LIKE…
Maurice Kroon
  • 959
  • 4
  • 13
  • 29
22
votes
4 answers

How to write a like query in HQL

I want to perform search for a particular string that is starting with a particular alphabet. So, for example if the starting alphabet is 'A' then it should produce a result which will contain all the strings with alphabet 'A'. How do I achieve this…
Ni3
  • 349
  • 1
  • 4
  • 8
22
votes
7 answers

MYSQL use 'LIKE' in 'WHERE' clause to search in subquery

How would you use 'LIKE' to search in a subquery? E.g. i've tried doing this, but doesn't work: SELECT * FROM mytable WHERE name LIKE '% (SELECT name FROM myothertable) %' I have this so far: SELECT * FROM t1 WHERE t1.name IN…
qwerty
  • 223
  • 1
  • 2
  • 5
20
votes
5 answers

How to do a LIKE considering two columns?

I have a customer table with two columns first_name and last_name. How can I use LIKE in a query being able to get data from both columns at same time? For instance: SELECT CONCAT(first_name, ' ', last_name) as 'full_name' FROM customer WHERE…
RedDragon
  • 2,080
  • 2
  • 26
  • 42
20
votes
2 answers

SQL Group by "Like"

I have a query where I generate our monthly customer contact activity. We have several categories (email out, email in, phone call in, phone call out, etc.) There are 8 distinct "type" results. I need to have two groups-one for all "email" and…
Jennifer Hart
  • 201
  • 1
  • 2
  • 3
20
votes
1 answer

Is it possible to convert enums to text in PostgreSQL

Is is possible to convert an enum declared in a postgresql schema to text so that I could use like clause for the enum column?
Blip
  • 3,061
  • 5
  • 22
  • 50
20
votes
2 answers

mysql like performance boost

is there anyway to speed up mysql like operator performance if wild card is involved? eg. like '%test%'
user121196
  • 30,032
  • 57
  • 148
  • 198
20
votes
3 answers

how to safely generate a SQL LIKE statement using python db-api

I am trying to assemble the following SQL statement using python's db-api: SELECT x FROM myTable WHERE x LIKE 'BEGINNING_OF_STRING%'; where BEGINNING_OF_STRING should be a python var to be safely filled in through the DB-API. I…
laramichaels
  • 1,515
  • 5
  • 18
  • 30
20
votes
4 answers

MySQL - Is it possible to use LIKE on all columns in a table?

I'm trying to make a simple search bar that searches through my database for certain words. It is possible to use the LIKE attribute without using WHERE? I want it to search all columns for the keywords, not just one. Currently I have…
user2566387
  • 211
  • 1
  • 2
  • 7
20
votes
3 answers

Using OR in LIKE Query in MySQL to compare multiple fields

I always thought that you could use OR in a LIKE statment to query things in MySQL. So, if I wanted to compare multiple fields in a row to 1 keyword or term: SELECT * FROM MyTable WHERE Column1 OR Column2 LIKE '%keyword%'; and if I had an array…
RCNeil
  • 8,581
  • 12
  • 43
  • 61
18
votes
3 answers

NOT LIKE ANY query in Snowflake

I am trying to run the following query in Snowflake: SELECT * FROM chapters WHERE title NOT LIKE ANY ('Summary%', 'Appendix%') but it errors out. I know Snowflake support LIKE ANY query syntax. But I am not sure why my query is not working.
Saqib Ali
  • 3,953
  • 10
  • 55
  • 100
18
votes
2 answers

Hibernate HQL query by using like operator

Seu the following mapping @Entity public class User { private Integer id; @Id; private Integer getId() { return this.id; } } Notice id is an Integer. Now i need this HQL query by using like operator Query query =…
Arthur Ronald
  • 33,349
  • 20
  • 110
  • 136