Unix Technical Forum

Newbie like question

This is a discussion on Newbie like question within the SQL Server forums, part of the Microsoft SQL Server category; --> Hi How do I run Multiple Likes in one query? Do I need to run each Like seperately? Thanks ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 09:30 AM
Driesen P via SQLMonster.com
 
Posts: n/a
Default Newbie like question


Hi

How do I run Multiple Likes in one query? Do I need to run each Like
seperately?

Thanks for any help.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 09:30 AM
Driesen P via SQLMonster.com
 
Posts: n/a
Default Re: Newbie like question


Sorry. I don't think that was clear. What I meant was I need something that
is similar to:

Select * from Security..SecUser
where UserId in (123, 456, 789)

The following query does not work:

Select * from Security..SecUser
where UserId like ('%123%', '%456%', '%789%')

Is there a query that can do this?

Thanks again
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 09:30 AM
Stu
 
Posts: n/a
Default Re: Newbie like question

You can use logical operators to join two tables; put the patterns you
want to match in one table, and join it to the source table. Here's an
example:

--Table with data that I want to look for a pattern in
DECLARE @Root TABLE (KeyVal int,
Root varchar(10))

INSERT INTO @Root
SELECT 1, 'ABCDEF'
UNION ALL
SELECT 2, 'DEFGHI'
UNION ALL
SELECT 3, '123ABC'
UNION ALL
SELECT 4, '123DEF'

--Table of patterns
DECLARE @LikeTest TABLE (LikeTest varchar(10))
INSERT INTO @LikeTest
SELECT 'ABC'
UNION ALL
SELECT 'DEF'
UNION ALL
SELECT 'XYZ'

--Results; note that I used DISTINCT to return a single value for each
match.
SELECT DISTINCT r.KeyVal, r.Root
FROM @Root r JOIN @LikeTest l ON r.Root LIKE '%' + l.LikeTest + '%'


HTH,
Stu

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-29-2008, 09:30 AM
ZeldorBlat
 
Posts: n/a
Default Re: Newbie like question

Alternatively:

Select * from Security..SecUser
where
UserId like '%123%'
or UserId like '%456%'
or UserId like '%789%'

Obviously, if you have a ton of them, it'll be a pain to type them all
out.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-29-2008, 09:30 AM
Driesen P via SQLMonster.com
 
Posts: n/a
Default Re: Newbie like question


Thanks for the help, guys. That was much appreciated!!


--
Message posted via SQLMonster.com
http://www.sqlmonster.com/Uwe/Forums...neral/200507/1
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 06:57 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com