Link to home
Start Free TrialLog in
Avatar of _TAD_
_TAD_

asked on

Combine "like" and "in" ??



Is there a way to combine the "like" and "in" commands?



Here is my current query:


Select * from table where col1 like 'A%' or col1 like 'B%'


I would prefer to have a query like:

select * from table where col1 in (like 'A%', like 'B%', 'Max')

Avatar of Scott Pletcher
Scott Pletcher
Flag of United States of America image

Nope, don't think you can do that.  AFAIK, you will have to code the LIKEs and the IN separately.
Avatar of _TAD_
_TAD_

ASKER



I figured as much, but it couldn't hurt to ask.


I'll leave this question open for a few more hours to see if anyone has found any wild or interesting way around this constraint.

Thanks!
select * from table where col1 in (Select * from table where col1 like 'A%', like 'B%', 'Max')
Avatar of _TAD_

ASKER


Mikal...

  I didn't think that was possible  >>>  col like 'A%', like '%B'      ...etc
select * from table where col1 in (Select col1 from table where col1 like 'A%', like 'B%', 'Max')

what do you mean?
FINAL QUERY

select * from table where col1 in (Select col1 from table where col1 like 'A%' or like 'B%' or col = 'Max')

Avatar of _TAD_

ASKER


Mikal

I'm still pretty sure that you can't do that.

This statement needs the word COL1 at least once more.

select * from table where col1 in (Select col1 from table where col1 like 'A%' or like 'B%' or col = 'Max')


besides, isn't that SQL statement a bit redundant?
did you try it?
I think this is what you're after:

select * from table where col1 in (Select col1 from table where col1 like 'A%'
                                                          or col1 like 'B%'
                                                          or col1 = 'Max')


try something like this

select * from table where substring(col1,1,1) in ('A', 'B') or Col1 ='MAX'

this works only if you want to search like 'A%','B%'..., if you want to search '%A%','%B%', i think no other way than to use multiple like conditions.

ASKER CERTIFIED SOLUTION
Avatar of PaulBarbin
PaulBarbin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of _TAD_

ASKER




PaulBarbin...

That almost looks like you are using something like Regular Expressions.



Does SQL server 2000 support regular expressions inside the query strings?

CREATE function  dbo.strSplitx ( @string varchar( 8000), @splitter char( 1) )
returns @res table ( id bigint primary key, rank bigint, val varchar( 8000) )
as
begin
      if substring ( @string, len ( @string), 1)<>@splitter
            set @string= @string+@splitter
      declare @start bigint, @word varchar( 8000), @charindex bigint, @i bigint
      set @i=1
      set @start=1
      set @charindex= charindex( @splitter, @string, @start)
      while (@charindex <> 0)begin
            set @word= substring( @string, @start, @charindex - @start)
            set @start= @charindex +1
            set @charindex= charindex( @splitter, @string, @start)
            insert into @res  values ( @start, @i, @word)
            set @i=@i+1
      end
      return
end
------
select * from dbo.strSplitx( 'A%,B%,MAX' , ',')   join  table on  ( col1 like val))
Yes, Like allows for regular expressions.  Look in BOL under LIKE, and then Pattern Matching in Searches.

Paul
Avatar of _TAD_

ASKER



Excellent!

The regular expressions isn't *exactly* what I wanted, but it does get me 90% of the way there.


I think if I play with the regular expressions a little more I may be able to hammer out my flaws.  

I have used regular expressions before, but I am not an expert.  With a little practice I think I'll get where I need to go.


Thanks everyone!