Solved

SQL 'IN' Statment Issue

Posted on 2008-11-01
12
294 Views
Last Modified: 2013-11-08
I am using the SQL Query Builder in VS08 - trying to get an IN statement to work - I put what I have come up with below.

I have tried putting everything I can think of in for @Status
'archive', 'open'
archive, open
'archive' 'open'
etc...

just archive (no quotes) seems to work in the Query Builder, but I need to select 3-4 status levels.\

HELP!

Thanks,

Andrew
Select * From Tickets Where Tickets.Status IN (@Status)

Open in new window

0
Comment
Question by:Crazy_Penguins
[X]
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
  • 4
  • 3
  • 2
  • +3
12 Comments
 
LVL 9

Expert Comment

by:jamesgu
ID: 22859128
in java, you can only bind known number of parameters, like ... in (?, ?,?) in case you have 3 parameters. This should be same in other languages

or you can build the whole sql string (like Select * From Tickets Where Tickets.Status IN ('archive', 'open')
 ) without parameter binding
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22859132
The problem is that the IN statement is looking for an array of values and you are passing one value that is comma delimited.

You will need to write a split function and then do this:

SELECT * FROM Tickets WHERE Tickets.Status IN (SELECT entry FROM dbo.split(@Status, ','))

Here is an example of split functions:
http://crossedlogic.blogspot.com/2008/09/converting-delimited-string-to-separate.html

Here is another from EE:
http:Q_23786715.html
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22859137
@Status is considered 1 single value, and not a list of values, which is the reason why it "does not work".

please create the below function, it will come in handy several times in your future, probably:

with @status having the value (no quotes, not spaces):
archive,open



Select * From Tickets Where Tickets.Status IN (select value from dbo.ParmsTolist(@Status,',') )
create FUNCTION dbo.ParmsToList (@Parameters varchar(500), @delimiter varchar(10) )
returns @result TABLE (Value varchar(8000))
AS  
begin
    declare @dx varchar(9)
    --declare @loops int
    -- set @loops = 0
 
     DECLARE @TempList table
          (
          Value varchar(8000)
          )
 
     if @delimiter is null  set @delimiter = ' '
     if datalength(@delimiter) < 1 set @delimiter = ' '
     set @dx = left(@delimiter, datalength(@delimiter)-1)
 
     DECLARE @Value varchar(8000), @Pos int
 
     SET @Parameters = @Parameters + @delimiter
     SET @Pos = CHARINDEX(@delimiter, @Parameters, 1)
 
     IF REPLACE(@Parameters, @delimiter, @dx) <> ''
     BEGIN
          WHILE @Pos > 0 --and @Loops < 100
          BEGIN
              -- set @loops = @loops + 1
               SET @Value = LEFT(@Parameters, @Pos - 1)
               IF @Value <> ''
               BEGIN
                    INSERT INTO @TempList (Value) VALUES (@Value) --Use Appropriate conversion
               END
               SET @Parameters = SUBSTRING(@Parameters, @Pos+ datalength(@delimiter),500)
               SET @Pos = CHARINDEX(@delimiter, @Parameters, 1)
 
          END
     END    
     INSERT @result
     SELECT value
        FROM @TempList
     RETURN
END  

Open in new window

0
Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

 
LVL 13

Accepted Solution

by:
sm394 earned 500 total points
ID: 22859160
try that

Select * From Tickets
WHERE  (charindex(rtrim(Tickets.Status ),@Status) > 0)
0
 

Author Closing Comment

by:Crazy_Penguins
ID: 31512389
sm394 wins - implemented the fastest / most easy.

0
 

Author Comment

by:Crazy_Penguins
ID: 22859182
sm394 wins - implemented the fastest / most easy.

I did also try mwvisa1 - could not get the function in - could be because I am using SQL 2000 on the back-end.
0
 
LVL 12

Expert Comment

by:jazzIIIlove
ID: 22859200
Hi there;

The SQL IN clause allows you to specify discrete values in your SQL WHERE search criteria as my friends dealt with examples...Let me add to clarify the usage:

Suppose we have the below table:

Employee               Date         Hours
John Smith             5/6/2004      8
Allan Babel             5/6/2004     8
Tina Crown            5/6/2004      8
John Smith             5/7/2004       9
Allan Babel             5/7/2004      8
Tina Crown            5/7/2004      10
John Smith              5/8/2004       8
Allan Babel             5/8/2004       8
Tina Crown             5/8/2004       9    

Consider the following SQL query using the SQL IN clause:
SELECT *
FROM EmployeeHours
WHERE Date IN ('5/6/2004', '5/7/2004')

This SQL expression will select only the entries where the column Date has value of '5/6/2004' or '5/7/2004', and you can see the result below:

Employee Date Hours
John Smith 5/6/2004 8
Allan Babel 5/6/2004 8
Tina Crown 5/6/2004 8
John Smith 5/7/2004 9
Allan Babel 5/7/2004 8
Tina Crown 5/7/2004 10

We can use the SQL IN statement with another column in our EmployeeHours table:

SELECT *
FROM EmployeeHours
WHERE Hours IN (9, 10)

The result of the SQL query above will be:

Employee Date Hours
John Smith 5/7/2004 9
Tina Crown 5/7/2004 10
Tina Crown 5/8/2004 9

>>in java, you can only bind known number of parameters, like ... in (?, ?,?) in case you have 3 >>parameters. This should be same in other languages

Good point, jamesgu is right...
PreparedStatement pstmt = con.prepareStatement("UPDATE EMPLOYEES
                                     SET SALARY = ? WHERE ID = ?");
pstmt.setBigDecimal(1, 153833.00)
pstmt.setInt(2, 110592)

But not only you can create known number of parameter

Statement stmt;
String sql;    
int rows;      

sql = "INSERT INTO tCust " 
    + "(custId, custName, custAddr) "
    + "VALUES "
    + "('" + custId   + "',"
    + "('" + custName + "',"
    + "('" + custAddr + "')";

stmt = theConn.createStatement();
rows = stmt.executeUpdate(sql);
theConn.dbConn.commit();
stmt.close();

Above the number of params known...

Best regards...

0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22859209
>sm394 wins - implemented the fastest / most easy.

except that you might run into issues... not on this concrete example, but in other cases.

say you have values in your column like:

archive
archived
open
not open

and you pass
archive,open

as argument. the selected solution will return all 4 rows, although you actually only want 2 of them...

0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22859214
second "problem": if you have an index on the field, the selected solution will NOT be able to use that index.
with the function suggestion of mvisa and me, that would still be possible, ie trying to avoid the full table scan...
0
 
LVL 13

Expert Comment

by:sm394
ID: 22859273
Just in case if you want to apply In Clause on IDs or exact values

DECLARE @SearchIn XML
SET @SearchIn ='<ROOT><ID>1</ID><ID>2</ID></ROOT>'

 Select * From Tickets Where Ticket.ID IN (
                                                                    SELECT  ParamValues.ID.value('.','int')
                                                                     FROM @SearchIn.nodes('//ID') as ParamValues(ID)
                                                                   )
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22859297
or simply modify slighty your previous syntax:
Select * From Tickets
WHERE  (charindex(rtrim( ',' + Tickets.Status + ',' ), ',' + @Status + ',') > 0)

Open in new window

0
 
LVL 13

Expert Comment

by:sm394
ID: 22859313
In your case

declare @Statusxml;
set @Status=convert(xml,N'<Root><Status>archive</Status><Status>open</Status></Root>');

Select * From Tickets Where Ticket.Status IN (
                                                                    SELECT  ParamValues.Status.value('.','nvarchar(50)')
                                                                     FROM @Status.nodes('//Status) as ParamValues(Status)
                                                                   )
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
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.

749 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