0

I was able to create my query by using Union. However I am wondering if there is an easier way to create the same results? I've tried using in ('abc%','def%') and like ('abc%','def%') however I get a syntax error near the comma separating the two words in parenthesis.

Here is what I have so far...

select distinct a.case,a.person,a.personid,b.plans,a.code,a.action,cast(a.datea as date) as datea,cast(a.dateb as date) as dateb,a.updatecode
from table(nolock) a

inner join table(nolock) b on a.column=b.column

where updatecode = 'invalid'

and code like ('pr%') and datea >= '2016-09-16' and len (b.plans) = 16

union

select distinct a.case,a.person,a.personid,b.plans,a.code,a.action,cast(a.datea as date) as datea,cast(a.dateb as date) as dateb,a.updatecode

from table(nolock) a

inner join table(nolock) b on a.column=b.column

where updatecode = 'invalid'

and code like ('sea%') and datea >= '2016-09-16' and len (b.plans) = 16
Machavity
  • 30,841
  • 27
  • 92
  • 100
H80TW
  • 31
  • 5
  • 1
    [Bad habits : Putting NOLOCK everywhere](http://blogs.sqlsentry.com/aaronbertrand/bad-habits-nolock-everywhere/) – sstan Sep 20 '16 at 12:49
  • 1
    `where updatecode = 'invalid' and (code like ('pr%') OR code like ('sea%')) and datea >= '2016-09-16' and len (b.plans) = 16` – artm Sep 20 '16 at 12:49
  • 2
    Possible duplicate of [Combining "LIKE" and "IN" for SQL Server](http://stackoverflow.com/questions/1865353/combining-like-and-in-for-sql-server) – Jesse Williams Sep 20 '16 at 12:54
  • No need to combine select distinct and union. – jarlh Sep 20 '16 at 13:08

3 Answers3

1

You can use:

(code like 'pr%' OR code like 'sea%')
Tab Alleman
  • 31,483
  • 7
  • 36
  • 52
1

I don't believe there's a good, clean-looking solution, but you can string multiple like queries with or, as in:

 select * from config.devices where displayname like 'Im%' or displayname like 'In%'

This will return all results that start with either "Im" or "In".

Jesse Williams
  • 653
  • 7
  • 21
1

Try this:

select distinct a.case,a.person,a.personid,b.plans,a.code,a.action,cast(a.datea as date) as datea,cast(a.dateb as date) as dateb,a.updatecode
from table(nolock) a
inner join table(nolock) b on a.column=b.column
where updatecode = 'invalid'
and (code like ('pr%') OR code like ('sea%'))
and datea >= '2016-09-16' 
and len (b.plans) = 16
Chaos Legion
  • 2,730
  • 1
  • 15
  • 14