Solved

TSQL "IN" fails when parameter is comma separated

Posted on 2011-03-18
4
467 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 142

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

776 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