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
9
votes
2 answers

SQL query to test if string value contains carriage return

Trying to figure out the right way to test if a VARCHAR column value ends with a carriage return. Tried this but it does not work, database is Oracle 11g... select name from myTable where name LIKE '%\r' OR name like '%\n'
raffian
  • 31,267
  • 26
  • 103
  • 174
8
votes
2 answers

LIKE wildcard with multiple fields and spaces in MYSQL

I'm having some trouble searching for any similar match in two fields. For example I have a table with the values: CAR MAKE CAR MODEL Ford Mustang (Shelby) Toyota Corolla Seat Leon etc etc. I want to be able to get the result…
penpen
  • 935
  • 3
  • 12
  • 22
8
votes
1 answer

How to prioritize a LIKE query select based on string position in field?

I am attempting to query a table for a limited resultset in order to populate an autocomplete field in javascript. I am, therefore, using a LIKE operator with the partial string entered. If I have, for example, a table such as: tblPlaces id …
lancsuni
  • 81
  • 2
8
votes
6 answers

SQL - searching database with the LIKE operator

Given your data stored somewhere in a database: Hello my name is Tom I like dinosaurs to talk about SQL. SQL is amazing. I really like SQL. We want to implement a site search, allowing visitors to enter terms and return relating records. A user…
Tom Gullen
  • 61,249
  • 84
  • 283
  • 456
8
votes
4 answers

How to use a LIKE query with CodeIgniter?

I want to run a query like this: SELECT * FROM table WHERE field LIKE '%search_term%' In CI you can bind parameters to queries, if you used field=? but this does not work for field LIKE "%?%". From debugging output it seems the query used is field…
DisgruntledGoat
  • 70,219
  • 68
  • 205
  • 290
8
votes
2 answers

Rails 4 - Prevent SQL injection using LIKE operator in SQL query

I'd like to use the following query for my Rails 4 application but am concerned about SQL injection attacks: @persons = People.where("persons.name LIKE ?", "%#{params[:search]}%") Can somebody show me the safe way to write the above statement? …
Vee
  • 1,821
  • 3
  • 36
  • 60
8
votes
2 answers

T-SQL special characters to escape for LIKE operator wildcard search

SQL Server has the LIKE operator to handle wildcard searches. My customer wants to use the "*" (asterisk) character in the user interface of an application as the wildcard character. I'm just wondering if there are any standard characters that I…
JustBeingHelpful
  • 18,332
  • 38
  • 160
  • 245
8
votes
4 answers

SQL LIKE operator with parameters and wildcards

I have a query where I want to return all Clients that have a certain string in the name with wildcards on either side. So the input could be "Smith" and i want to return all things like "The John Smith Company" or "Smith and Bros". I want [Client]…
Chris568524
  • 123
  • 1
  • 2
  • 10
8
votes
4 answers

Simulating LIKE in PHP

Is there a way to simulate the LIKE operator of SQL in PHP with the same syntax? (% and _ wildcards and a generic $escape escape character)? So that having: $value LIKE $string ESCAPE $escape you can have a function that returns the PHP evaluation…
Shoe
  • 74,840
  • 36
  • 166
  • 272
7
votes
3 answers

On SQL Server (2008), if I want to filter a string field that starts with something, what is the best way?

On several SQL queries I need to check if a field starts with a character. There are several ways to do it, which one is better in performance/standard? I usually use tb.field LIKE 'C%' but I can also use LEFT(LTRIM(tb.Field),1) = 'C' I know well…
Mattews
  • 73
  • 1
  • 1
  • 4
7
votes
2 answers

MYSQL SELECT WHERE LIKE WITH AES_ENCRYPT

How would I perform a Mysql SELECT with WHERE and LIKE serach if field is AES_ENCYPTED? Example: SELECT AES_DECRYPT(place,'"+salt+"'),AES_DECRYPT(web_address,'"+salt+"') FROM access WHERE place= LIKE…
user963206
  • 83
  • 1
  • 1
  • 3
7
votes
3 answers

How do apply SQL like on "\detail1\detail2\" (Escaping '\')?

I have a table T1 with a column details, data column has following sample data : select details from T1; \this is detail 1\this is detail2\ \this is detail 1\this is detail2\ :this is detail 1:this is detail2: :this is detail 1:this is…
Yugal Jindle
  • 44,057
  • 43
  • 129
  • 197
7
votes
4 answers

Use of Like * Works in MS-Access but Not VBA

I have a simple query but am running into problems using LIKE in VBA. My SQL string in VBA is: stsql1 = "Select Top 25 data.* from data where data.Description Like ('*') " When I run this sql string in my VBA code I get no records returned, but if…
Rich Estabrook
  • 71
  • 1
  • 1
  • 2
7
votes
7 answers

How to make "LIKE" query work in MongoDB?

I have a list of street names and I want to select all that start with "Al". In my MySQL I would do something like SELECT * FROM streets WHERE "street_name" LIKE "Al%" How about MongoDB using PHP?
jM2.me
  • 3,839
  • 12
  • 44
  • 58
7
votes
4 answers

LINQ to SQL - select where text like string array

I have a List of variable count, and I want to query (via LINQ) a table to find any items that contain any of those strings in the Text column. Tried this (doesn't work): items = from dbt in database.Items where…
bjallen
  • 73
  • 1
  • 1
  • 4