Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. SQL Server
  3. Question
SQL Server

How to select like with multiple values in sql?

Asked by Bhushan Bhad Sep 29, 2022 4.8K views 2 answers
Share

About this question

 I have a SQL query given below, I want to select multiple values using the like operator.

Is my Query correct?

SELECT top 1 employee_id, employee_ident, utc_dt, rx_dt 

FROM       employee

INNER JOIN employee_mdata_history 

ON         employee.ident=employee_mdata_history.employee_ident 
WHERE      employee_id like 'emp1%' , 'emp3%' 
ORDER BY   rx_dt desc

If not, can anyone correct me?

My table has a large amount of data starting with 'emp1' and 'emp3'. Can I filter the result by top 3 "emp1" and top 2 "emp3" based on rx_dt?

Your answer

2 Answers

Ranjana Admin JanBask Expert Latest answer

Answered on May 8, 2024

In SQL, you can use the LIKE operator with multiple values by using the OR logical operator to specify multiple conditions. Here's an example of how you can use LIKE with multiple values:

SELECT *
FROM your_table
WHERE your_column LIKE '%value1%' OR your_column LIKE '%value2%' OR your_column LIKE '%value3%';

In this query:

  • your_table is the name of your table.
  • your_column is the column in which you want to search for values.
  • %value1%, %value2%, and %value3% are the patterns you want to search for. % is a wildcard that matches any sequence of characters.

Replace your_table and your_column with the actual table and column names from your database schema, and replace value1, value2, value3, etc., with the actual values you want to search for.

Alternatively, you can use the IN operator along with LIKE to achieve a similar result:

SELECT *
FROM your_table
WHERE your_column LIKE '%value1%' 
   OR your_column LIKE '%value2%' 
   OR your_column LIKE '%value3%';

This query will return all rows where your_column contains any of the specified values (value1, value2, value3, etc.).

Was this helpful?

More SQL Server discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest SQL Server Blogs

Guides, tips and career advice on SQL Server from JanBask experts.