In clause in SQL Query

Hi,
In the below two queries, first one returns 2 records and the second one returns no records. How do I make the second query work?

--Query 1
Select * from MediaOwners where MediaOwnerName in ('Viacom','Lamar')

--Query 2
declare @x varchar(100)
set @x='''Viacom'',''Lamar'''
print @x
Select * from MediaOwners where MediaOwnerName in (@x)
KaruppaiahAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

robertjbarkerCommented:
Instead of seaching for 'Viacom' or 'Lamar' you are searching for the single string 'Viacom','Lamar'. One way to solve the problem is put the individual strings in a temp table:


declare @x varchar(100)
create table #searchlist (item varchar(100))
insert #searchlist (item) values ('Viacom')
insert #searchlist (item) values ('Lamar')
Select * from MediaOwners where MediaOwnerName in (select * from #searchlist)
drop table #searchlist
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
robertjbarkerCommented:
Another way:

declare @x varchar(100)
set @x='''Viacom'',''Lamar'''

declare @xs varchar(1000)
set @xs = 'Select * from MediaOwners where MediaOwnerName in (' + @x +')'
exec (@xs)
0
illCommented:
-- one more possibility, if @x members are unique
declare @x varchar(100)
set @x='''Viacom'',''Lamar'''
print @x
Select * from MediaOwners where @x like ( '%'''+ MediaOwnerName + '''% ')
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

LowfatspreadCommented:
or of course parse the inlist string into separate components
(if you'll allow only a fixed number) and

select *
from ..
where yyy in (@x1.@x2,@x3,...),
0
fds_fatboyCommented:
How about something like (bear with the typos, I haven't got SQL Server on this machine)

--First create a new (reusable UDF) to split the string up.
CREATE FUNCTION dbo.fnSplitString(@MyString varchar(8000),@delimiter char(1))
RETURNS @SplitString TABLE
(
    string varchar(8000)
)
AS
BEGIN
    DECLARE @chindex int
    @chindex = CHARINDEX(@delimiter,@MyString)
    WHILE @chindex > 0
    BEGIN
        INSERT INTO @SplitString
        VALUES(LEFT(@MyString,@chIndex -1))
        SET @MyString = SUBSTRING(@MyString,chIndex+1,8000)
    END
    INSERT INTO @SplitString
    VALUES(@MyString)
    RETURN
END

-- Then alter your original query thus:
declare @x varchar(100)
set @x='Viacom,Lamar'
print @x
Select * from MediaOwners where MediaOwnerName in (SELECT string FROM dbo.fnSplitString(@x), ",")


0
fds_fatboyCommented:
Sorry, that last line should have read:

Select * from MediaOwners where MediaOwnerName in (SELECT string FROM dbo.fnSplitString(@x.','))
0
volkingCommented:
hmmmm ... wouldn't this work ... just build a TSQL string composed with the appropriate quotes and execute?

declare @MySqlRaw varchar(1000)
SET @MySqlRaw = 'Select * from MediaOwners where MediaOwnerName in (' + ''''Viacom'',''Lamar''' + ')'
EXEC (@MySqlRaw)

(p.s. not sure I have the single quotes balanced in above but you get the idea)

0
Anthony PerkinsCommented:
>>wouldn't this work<<
Yes, but it requires using dynamic SQL.  This implies bad performance and lousy security (you must give the user Select permissions for the table).
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.