How can like be selected with multiple values in SQL?

199    Asked by dhanan_7781 in SQL Server , Asked on Sep 14, 2023

 I intend to select multiple values using the SQL query below. But I want to do this by using this like an operator. 

SELECT top 1 employee_id, employee_ident, utc_dt, rx_dt 

Here is another SQL query from the table employee. 

ON         employee.ident=employee_mdata_history.employee_ident 

WHERE      employee_id like 'emp1%' , 'emp3%' 

ORDER BY   rx_dt desc

The data table has many data like ‘emp1’ and ‘emp3’, etc. Can I filter the result by top 3 ‘emp1’ and top 2 ‘emp3’, depending on the rx_dt? 

Answered by Dan Peters

 If you want to select SQL-like multiple values, I can guess that you want a row dedicated to employee_id (emp1%) and another row dedicated to employee_id (emp3%). You can follow the following command: 

(SELECT top 1 employee_id, employee_ident, utc_dt, rx_dt

FROM employee



)AS t1


UNION ALL


SELECT t2.*


FROM 



)AS t2;

You can use UNION ALL because the legs in the union can be disjointed. It can be a performance advantage. Also, since SQL-server 2008 allows undow functions, for example, row_number(), you can also run the following query. 



Your Answer

Interviews

Parent Categories