0

I have a very interesting problem. I have an SSRS report with a multiple select drop down. The drop down allows to select more than one value, or all values. All values is not the problem.

The problem is 1 or the combination of more than 1 option

When I select in the drop down 'AAA' it should return 3 values: 'AAA','AAA 1','AAA 2'

Right now is only returning 1 value.

QUESTION:

How can make the IN statement work like a LIKE?

The Drop down select

SELECT '(All)' AS team, '(All)' AS Descr 
UNION ALL
SELECT 'AAA' , 'AAA' 
UNION ALL
SELECT 'BBB' , 'BBB'  


Table Mytable

ColumnA Varchar(5)

Values for ColumnA
'AAA'
'AAA 1'
'AAA 2'
'BBB'
'BBB 1'
'BBB 2'


SELECT * FROM Mytable
WHERE ColumnA IN (SELECT * FROM SplitListString(@Team, ',')))


Split function

CREATE FUNCTION [dbo].[SplitListString]
 (@InputString NVARCHAR(max), @SplitChar CHAR(1))
 RETURNS @ValuesList TABLE
 (
 param NVARCHAR(MAX)
 )
 AS
 BEGIN

    DECLARE @ListValue     NVARCHAR(max)
    DECLARE @TmpString     NVARCHAR(max) 
    DECLARE @PosSeparator  INT  
    DECLARE @EndValues     BIT

    SET @TmpString = LTRIM(RTRIM(@InputString));
    SET @EndValues = 0

    WHILE (@EndValues = 0) BEGIN
        SET @PosSeparator = CHARINDEX(@SplitChar, @TmpString)

        IF (@PosSeparator) > 1 BEGIN
            SELECT @ListValue = LTRIM(RTRIM(SUBSTRING(@TmpString, 1, @PosSeparator -1 )))
        END
        ELSE BEGIN
            SELECT @ListValue = LTRIM(RTRIM(@TmpString))
            SET @EndValues = 1
        END

        IF LEN(@ListValue) > 0 BEGIN
            INSERT INTO @ValuesList
            SELECT @ListValue       
        END

        SET @TmpString = LTRIM(RTRIM(SUBSTRING(@TmpString, @PosSeparator + 1, LEN(@TmpString) - @PosSeparator)))
    END

    RETURN
END
2
  • Can you post a bit of SplitListString()? Also, this may be a stretch, but your values aren't all 3 characters long followed by a digit?
    – Kermit
    Aug 22, 2012 at 18:07
  • I could but it will not make a difference since the problem is in the select and where clause. The split string only creates a table separated by commas Aug 22, 2012 at 18:11

3 Answers 3

3

You can't. But, you can make the like work like the like:

select *
from mytable t join
     SplitListString(@Team, ',') s
     on t.ColumnA like '%'+s.param+'%'

That is, move the split list to an explicit join. Replace with the actual column name returned by the function, and use the like function.

Or, if you prefer:

select *
from mytable t cross join
     SplitListString(@Team, ',') s
where t.ColumnA like '%'+s.param+'%'

The two versions are equivalent and should produce the same execution plan.

11
  • It will be very hard to do a join. This has to go in the where clause Aug 22, 2012 at 18:09
  • Yes, if they select "ALL" then there is not need to JOIN. Aug 22, 2012 at 18:12
  • @InternetEngineer . . . if you prefer to cross join and put the condition in the WHERE clause, you can do that. You don't need to. The query works either way. Aug 22, 2012 at 18:15
  • @Tamir . . . The question is explicitly about functionaltiy, not performance. This is how you can make the "IN" into a "LIKE". Each comparison requires a separate pass through the data table, and indexes can't be used. Whether this is acceptable or not seems unrelated to how it can be done. Aug 22, 2012 at 18:18
  • 1
    @InternetEngineer . . . .suggestion: ask another question, and give more information about what you are trying to do. There might be a better way to accomplish what you want. Aug 22, 2012 at 18:40
1

Better approach would be to have a TeamsTable (teamID, teamName, ...) and teamMembersTable (teamMemberID, teamID, teamMemberDetails, ...).
Then you an build your dropdown list as
SELECT ... FROM TeamsTable ...;
and
SELECT ... FROM teamMembersTable WHERE teamID IN (valueFromYourDropDown);

Or you can just store your teamID or teamName (or both) in your (equivalent of) teamMembersTable

2
  • This does not work as the drop down allows multiple selections and the target table needs to do a like search. Aug 22, 2012 at 18:32
  • I misspelled that, code above should have read: teamID IN (valuesFromYourDropDown); (multiple valueS) - this was the reason for IN clause. From parts of your code I assumed (possibly incorrectly) that you have a drop-down of teams/groups and then you want to look for "members" of these teams/groups. If that IS the intention than storing a groupID/Name on each record IS the best way of doing it. This way you can add more groups and re-group existing members just by manipulating data in your table(s). Aug 22, 2012 at 21:42
0

You're not going to get IN to work the same as LIKE without a lot of work. You could do something like this though (and it would be nice to see some of your actual data though so we could give better solutions):

SELECT *
FROM table
WHERE LEFT(field,3) IN @Parameter

If you'd like better performance, create a code field on your table and update it like this:

UPDATE table
SET codeField = LEFT(field,3)

Then just add an index on that field and run this query to get your results:

SELECT *
FROM table
WHERE codeField IN @Parameter
2
  • The data I placed in the sample is very close to the real thing. Aug 22, 2012 at 18:34
  • Ok, so if you have a set number of items in your drop down, and you're looking for fields that start with those items, then I don't see why what Germann or I suggested wouldn't work.
    – Jim
    Aug 22, 2012 at 18:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.