Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server using wildcard within IN

Since I believe this should be a basic question I know this question has probably been asked, but I am unable to find it. I'm probably about to earn my Peer Pressure badge, but I'll ask anyway:

Is there a way in SQL Server that I am not aware of for using the wildcard character % when using IN.

I realize that I can use OR's like:

select * from jobdetails where job_no like '0711%' or job_no like '0712%' 

and in some cases I can use a subquery like:

select * from jobdetails where job_no in (select job_no from jobs where job_id = 39) 

but I'm looking to do something like the following:

select * from jobdetails where job_no in ('0711%', '0712%') 

In this case it uses the percent sign as a character instead of a wildcard character so no rows are returned. I currently just use a bunch of OR's when I have to do this, but I know there has to be a better way. What method do you use for this?

like image 526
Dusty Avatar asked Jul 02 '09 18:07

Dusty


People also ask

Can you use a wildcard in an in statement SQL?

Summary. To broaden the selections of a structured query language (SQL-SELECT) statement, two wildcard characters, the percent sign (%) and the underscore (_), can be used.

How do I use a wildcard in SQL?

SQL Wildcards A wildcard character is used to substitute one or more characters in a string. Wildcard characters are used with the LIKE operator. The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.

How do I use multiple wildcards in SQL?

The SQL LIKE clause is used to compare a value to similar values using wildcard operators. There are two wildcards used in conjunction with the LIKE operator. The percent sign represents zero, one or multiple characters. The underscore represents a single number or character.


1 Answers

How about:

WHERE LEFT(job_no, 4) IN ('0711', '0712', ...) 
like image 123
Aaron Alton Avatar answered Sep 29 '22 14:09

Aaron Alton