How to select like with multiple values in sql?

3.2K    Asked by bhusha_8629 in SQL Server , Asked on Sep 29, 2022

 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?

Answered by Brian Kennedy

To select like with multiple values in sql -


I guess you want 1 row where employee_id like 'emp1%' and another where employee_id like 'emp3%'. One way to achieve this is to use a union:

SELECT t1.*

FROM

  ( SELECT top 1 employee_id, employee_ident, utc_dt, rx_dt

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

  ) AS t1

UNION ALL

SELECT t2.*

FROM

  ( SELECT top 1 employee_id, employee_ident, utc_dt, rx_dt

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

  ) AS t2 ;

Since the legs in the union are guaranteed to be disjoint, a UNION ALL can be used, and that might be a performance advantage compared to using just a UNION.

I believe SQL-server 2008 supports window functions like row_number(), so you can use something like:

SELECT employee_id, employee_ident, utc_dt, rx_dt

FROM (

    SELECT employee_id, employee_ident, utc_dt, rx_dt

      , row_number() over (partition by substring(employee_id,1,4)
                           order by rx_dt desc) as rn

    FROM employee

    JOIN employee_mdata_history

        ON employee.ident = employee_mdata_history.employee_ident 
    WHERE employee_id like 'emp1%'
       OR employee_id like 'emp3%'

) as T

WHERE rn = 1

ORDER BY rx_dt desc;



Your Answer

Answer (1)

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.).

7 Months

Interviews

Parent Categories