Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

TSQL "IN" fails when parameter is comma separated

Posted on 2011-03-18
4
473 Views
Last Modified: 2012-05-11
SELECT with IN clause failes when values are comma separated....

DECLARE @MYVAR AS Varchar(50)
SET @MYVAR = 'FL, AK, NY, CA'

SELECT * FROM MYTABLE
WHERE
(@MYVAR IS NULL OR [State] = @MYVAR)
0
Comment
Question by:Internet_Engineer
  • 2
4 Comments
 
LVL 32

Expert Comment

by:ewangoya
ID: 35168518
Try this way

DECLARE @MYVAR AS Varchar(50)
SET @MYVAR = 'FL, AK, NY, CA'

SELECT * FROM MYTABLE
WHERE @MYVAR IS NULL
OR [State] IN (@MYVAR)
0
 
LVL 32

Accepted Solution

by:
ewangoya earned 500 total points
ID: 35168553
or

Try this way

DECLARE @MYVAR AS Varchar(50)
SET @MYVAR = 'FL, AK, NY, CA'

SELECT * FROM MYTABLE
WHERE @MYVAR IS NULL
OR CHARINDEX([State], @MYVAR, 1) > 0
0
 

Author Closing Comment

by:Internet_Engineer
ID: 35168583
GREAT
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35168951
please check out this article, it has more performant options:
http://www.experts-exchange.com/A_1536.html
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Server 2008 R2, need a pivot/cross tab query... 4 28
SQL server 2014 replication error 16 34
SQL R 21 26
Parse this column 6 27
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question